diff --git a/NAMESPACE b/NAMESPACE index b6c1df0..7eac36c 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -23,6 +23,7 @@ export(is_link) export(italicsr) export(latexr) export(listr) +export(markdownify) export(olistr) export(remedy_example) export(remedy_opts) diff --git a/R/markdownifyr.R b/R/markdownifyr.R new file mode 100644 index 0000000..9e75d50 --- /dev/null +++ b/R/markdownifyr.R @@ -0,0 +1,44 @@ +#' @title markdownify +#' +#' @description Convert selected text from HTML to markdown +#' +#' @return markdown text +#' @export +#' @importFrom rstudioapi getSourceEditorContext modifyRange +markdownify <- function() { + + context <- rstudioapi::getSourceEditorContext() + txt <- context$selection[[1]]$text + + looks_like_anchor <- grepl("[^<]+", txt)[1] + looks_like_bold <- grepl("[^<]+", txt)[1] + looks_like_image <- grepl("(.*?)', '[\\2](\\1)', txt) + if (looks_like_italic) content <- gsub('(.*?)', '_\\1_', txt) + if (looks_like_bold) content <- gsub('(.*?)', '\\*\\*\\1\\*\\*', txt) + if (looks_like_image) content <- gsub('', '![](\\1)', txt) + if (looks_like_image_alt) { + alt <- gsub('', "\\1", txt) + content <- gsub('!\\[\\]', paste0('![', alt, ']'), content) + } + + rstudioapi::modifyRange(location = context$selection[[1]]$range, text = content, id = context$id) + +} + diff --git a/README.Rmd b/README.Rmd index 5a46416..cb32fef 100644 --- a/README.Rmd +++ b/README.Rmd @@ -140,6 +140,20 @@ Turn the selected text into an unordered list. ![](reference/figures/list.gif) +### Markdownify + +If you have existing HTML tags (e.g. for pages exported from elsewhere) you can +convert these to markdown syntax with the Markdownify Addin + +``` +turns this into _this_ +turns this into **this** +turns this into [this](https://sub.domain.mylink.html) +turns into ![](~/dir/image.png) +turns with text into ![with text](~/dir/image.png) +leaves normal text unchanged +``` + ### Moving #### On the right diff --git a/README.md b/README.md index cdf593b..417ca58 100644 --- a/README.md +++ b/README.md @@ -1,38 +1,48 @@ -[![CRAN\_Status\_Badge](http://www.r-pkg.org/badges/version/remedy)](https://cran.r-project.org/package=remedy) [![](http://cranlogs.r-pkg.org/badges/remedy)](https://cran.r-project.org/package=remedy)[![Travis-CI Build Status](https://travis-ci.org/ThinkR-open/remedy.svg?branch=master)](https://travis-ci.org/ThinkR-open/remedy)[![Coverage status](https://codecov.io/gh/ThinkR-open/remedy/branch/master/graph/badge.svg)](https://codecov.io/github/ThinkR-open/remedy?branch=master)[![Covrpage Summary](https://img.shields.io/badge/covrpage-Last_Build_2018_12_21-brightgreen.svg)](http://tinyurl.com/y76olv3v) -remedy -====== +[![CRAN\_Status\_Badge](http://www.r-pkg.org/badges/version/remedy)](https://cran.r-project.org/package=remedy) +[![](http://cranlogs.r-pkg.org/badges/remedy)](https://cran.r-project.org/package=remedy)[![Travis-CI +Build +Status](https://travis-ci.org/ThinkR-open/remedy.svg?branch=master)](https://travis-ci.org/ThinkR-open/remedy)[![Coverage +status](https://codecov.io/gh/ThinkR-open/remedy/branch/master/graph/badge.svg)](https://codecov.io/github/ThinkR-open/remedy?branch=master)[![Covrpage +Summary](https://img.shields.io/badge/covrpage-Last_Build_2018_12_21-brightgreen.svg)](http://tinyurl.com/y76olv3v) -`{remedy}` provides addins to facilitate writing in markdown with RStudio. +# remedy + +`{remedy}` provides addins to facilitate writing in markdown with +RStudio. ![](reference/figures/remedy_example.gif) -All the functions are meant to be mapped to keyboard shortcuts. A list of suggested shortcuts is provided towards the end of this README. +All the functions are meant to be mapped to keyboard shortcuts. A list +of suggested shortcuts is provided towards the end of this README. -> Note that most of the addins/shortcuts below will also work without selecting any text. +> Note that most of the addins/shortcuts below will also work without +> selecting any text. -Install -------- +## Install ``` r remotes::install_github("ThinkR-open/remedy") ``` -Once you've installed the package, you don't need to load it with `library()`, the addins are installed on your machine as part of the package install process. +Once you’ve installed the package, you don’t need to load it with +`library()`, the addins are installed on your machine as part of the +package install process. -Using `{remedy}` ----------------- +## Using `{remedy}` -Write quicker in markdown with `{remedy}`! -Read the full manual in the associated {pkgdown} website: +Write quicker in markdown with `{remedy}`\! +Read the full manual in the associated {pkgdown} website: + -Here's a list of all available helpers: +Here’s a list of all available helpers: ![](reference/figures/remedy_example.gif) -You can also have an overview of how the functions behave with the `remedy_example()` function: +You can also have an overview of how the functions behave with the +`remedy_example()` function: ``` r library(remedy) @@ -101,9 +111,11 @@ Turn the text into an html comment Turn the selected path into an image. -This element is context aware: if you select a text and a link, it turns the text into title between `![]`, and puts the link between `()`. +This element is context aware: if you select a text and a link, it turns +the text into title between `![]`, and puts the link between `()`. -If the last element of the selection is not a link, you get an error message straight into you markdown document. +If the last element of the selection is not a link, you get an error +message straight into you markdown document. ![](reference/figures/image.gif) @@ -119,6 +131,18 @@ Turn the selected text into an unordered list. ![](reference/figures/list.gif) +### Markdownify + +If you have existing HTML tags (e.g. for pages exported from elsewhere) +you can convert these to markdown syntax with the Markdownify Addin + + turns this into _this_ + turns this into **this** + turns this into [this](https://sub.domain.mylink.html) + turns into ![](~/dir/image.png) + turns with text into ![with text](~/dir/image.png) + leaves normal text unchanged + ### Moving #### On the right @@ -147,9 +171,11 @@ Turn your dataframe into a markdown table : Turn the selected text into a link. -This element is context aware: if you select a text and a link, it turns the text into title between `[]`, and puts the link between `()`. +This element is context aware: if you select a text and a link, it turns +the text into title between `[]`, and puts the link between `()`. -If the last element of the selection is not a link, you get an error message straight into you markdown document. +If the last element of the selection is not a link, you get an error +message straight into you markdown document. ![](reference/figures/url.gif) @@ -165,48 +191,51 @@ Insert a xaringan pull-left and pull-right template. ![](reference/figures/xaringan.gif) -Recommended shortcuts (macOS) ------------------------------ +## Recommended shortcuts (macOS) -Here's a list of recommended shortcuts: +Here’s a list of recommended shortcuts: ``` r remedy::remedy_opts$get('hotkeys') #> backtick bold chunk #> "Ctrl+Cmd+`" "Ctrl+Cmd+B" "Ctrl+Alt+Cmd+C" -#> chunksplit footnote h1 -#> "Ctrl+Shift+Alt+C" "Ctrl+Cmd+Shift+6" "Ctrl+Cmd+1" -#> h2 h3 h4 -#> "Ctrl+Cmd+2" "Ctrl+Cmd+3" "Ctrl+Cmd+4" -#> h5 h6 htmlcomment -#> "Ctrl+Cmd+5" "Ctrl+Cmd+6" "Ctrl+Alt+C" -#> image italics latex -#> "Ctrl+Cmd+P" "Ctrl+Cmd+I" "Ctrl+Cmd+L" -#> list right strike -#> "Ctrl+Shift+Cmd+=" "Alt+Cmd+Right" "Ctrl+Cmd+S" -#> table url xaringan -#> "Ctrl+Cmd+T" "Ctrl+Cmd+U" "Ctrl+Cmd+X" -#> youtube -#> "Ctrl+Cmd+Y" +#> chunksplit chunkname footnote +#> "Ctrl+Shift+Alt+C" "Ctrl+Shift+Alt+N" "Ctrl+Cmd+Shift+6" +#> h1 h2 h3 +#> "Ctrl+Cmd+1" "Ctrl+Cmd+2" "Ctrl+Cmd+3" +#> h4 h5 h6 +#> "Ctrl+Cmd+4" "Ctrl+Cmd+5" "Ctrl+Cmd+6" +#> htmlcomment image italics +#> "Ctrl+Alt+C" "Ctrl+Cmd+P" "Ctrl+Cmd+I" +#> latex list right +#> "Ctrl+Cmd+L" "Ctrl+Shift+Cmd+=" "Alt+Cmd+Right" +#> strike table url +#> "Ctrl+Cmd+S" "Ctrl+Cmd+T" "Ctrl+Cmd+U" +#> xaringan youtube +#> "Ctrl+Cmd+X" "Ctrl+Cmd+Y" ``` ### Managing Shortcuts -Use the [`{rsam}`](https://github.com/yonicd/rsam) package to set and manage for you the hotkey settings of `{remedy}`. +Use the [`{rsam}`](https://github.com/yonicd/rsam) package to set and +manage for you the hotkey settings of `{remedy}`. -If you want to edit the default settings you can view the defaults `remedy_opts$get('hotkeys')` and change them through `remedy_opts$set(hotkeys=)`. +If you want to edit the default settings you can view the defaults +`remedy_opts$get('hotkeys')` and change them through +`remedy_opts$set(hotkeys=)`. ``` r hotkeys <- remedy::remedy_opts$get("hotkeys") rsam::set_shortcut(sprintf('remedy::%sr',names(hotkeys)), hotkeys) ``` -Feedback and enhancement ------------------------- +## Feedback and enhancement -You've found a bug, or have an enhancement idea? Feel free to open an issue : . +You’ve found a bug, or have an enhancement idea? Feel free to open an +issue : . -Code of conduct ---------------- +## Code of conduct -Please note that the 'remedy' project is released with a [Contributor Code of Conduct](CODE_OF_CONDUCT.md). By contributing to this project, you agree to abide by its terms. +Please note that the ‘remedy’ project is released with a [Contributor +Code of Conduct](CODE_OF_CONDUCT.md). By contributing to this project, +you agree to abide by its terms. diff --git a/inst/rstudio/addins.dcf b/inst/rstudio/addins.dcf index 3e8b8cc..e073ba4 100644 --- a/inst/rstudio/addins.dcf +++ b/inst/rstudio/addins.dcf @@ -103,6 +103,11 @@ Description: Convert selected text into an unordered list Binding: listr Interactive: false +Name: Markdownify +Description: Convert selected text from HTML into markdown +Binding: markdownify +Interactive: false + Name: Right Description: Copy selected text (or line) to the right Binding: rightr diff --git a/man/markdownify.Rd b/man/markdownify.Rd new file mode 100644 index 0000000..9b15f7c --- /dev/null +++ b/man/markdownify.Rd @@ -0,0 +1,14 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/markdownifyr.R +\name{markdownify} +\alias{markdownify} +\title{markdownify} +\usage{ +markdownify() +} +\value{ +markdown text +} +\description{ +Convert selected text from HTML to markdown +}