Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export(is_link)
export(italicsr)
export(latexr)
export(listr)
export(markdownify)
export(olistr)
export(remedy_example)
export(remedy_opts)
Expand Down
44 changes: 44 additions & 0 deletions R/markdownifyr.R
Original file line number Diff line number Diff line change
@@ -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("<a[ ]+href", txt)[1]
looks_like_italic <- grepl("<i>[^<]+</i>", txt)[1]
looks_like_bold <- grepl("<b>[^<]+</b>", txt)[1]
looks_like_image <- grepl("<img[ ]+src", txt)[1]
looks_like_image_alt <- looks_like_image && grepl("alt=", txt)[1]

if (
!(
looks_like_anchor |
looks_like_italic |
looks_like_bold |
looks_like_image
)
) {
warning("Not able to convert this text to markdown, not recognised", call. = FALSE)
return(invisible(NULL))
}


if (looks_like_anchor) content <- gsub('<a href="([^"]+?)">(.*?)</a>', '[\\2](\\1)', txt)
if (looks_like_italic) content <- gsub('<i>(.*?)</i>', '_\\1_', txt)
if (looks_like_bold) content <- gsub('<b>(.*?)</b>', '\\*\\*\\1\\*\\*', txt)
if (looks_like_image) content <- gsub('<img src="([^"]*?)".*?>', '![](\\1)', txt)
if (looks_like_image_alt) {
alt <- gsub('<img.*?alt="([^"]+?)".*?>', "\\1", txt)
content <- gsub('!\\[\\]', paste0('![', alt, ']'), content)
}

rstudioapi::modifyRange(location = context$selection[[1]]$range, text = content, id = context$id)

}

14 changes: 14 additions & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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 <i>this</i> into _this_
turns <b>this</b> into **this**
turns <a href="https://sub.domain.mylink.html">this</a> into [this](https://sub.domain.mylink.html)
turns <img src="~/dir/image.png"> into ![](~/dir/image.png)
turns <img src="~/dir/image.png" alt="with text"> into ![with text](~/dir/image.png)
leaves normal text unchanged
```

### Moving

#### On the right
Expand Down
117 changes: 73 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,48 @@

<!-- README.md is generated from README.Rmd. Please edit that file -->
[![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 <img src="https://github.com/ThinkR-open/remedy/blob/master/reference/figures/thinkr-hex-remedy-small.png?raw=true" align="right"/>

`{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: <https://thinkr-open.github.io/remedy/>
Write quicker in markdown with `{remedy}`\!
Read the full manual in the associated {pkgdown} website:
<https://thinkr-open.github.io/remedy/>

Here's a list of all available helpers:
Heres 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)
Expand Down Expand Up @@ -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)

Expand All @@ -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 <i>this</i> into _this_
turns <b>this</b> into **this**
turns <a href="https://sub.domain.mylink.html">this</a> into [this](https://sub.domain.mylink.html)
turns <img src="~/dir/image.png"> into ![](~/dir/image.png)
turns <img src="~/dir/image.png" alt="with text"> into ![with text](~/dir/image.png)
leaves normal text unchanged

### Moving

#### On the right
Expand Down Expand Up @@ -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)

Expand All @@ -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:
Heres 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=<NEW_SETTINGS>)`.
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=<NEW_SETTINGS>)`.

``` 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 : <https://github.com/ThinkR-open/remedy/issues>.
You’ve found a bug, or have an enhancement idea? Feel free to open an
issue : <https://github.com/ThinkR-open/remedy/issues>.

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.
5 changes: 5 additions & 0 deletions inst/rstudio/addins.dcf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions man/markdownify.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.