New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Hugo Shortcodes in Rmd files #40

Closed
mwhitaker opened this Issue Jan 17, 2017 · 20 comments

Comments

Projects
None yet
5 participants
@mwhitaker

mwhitaker commented Jan 17, 2017

Thank you for a great package! A really nice feature of Hugo is shortcodes. When Hugo encounters these snippets in a markdown file it renders small HTML templates into the output HTML file, eg

{{< tweet 820477439050731521 >}}

would embed the tweet into the output HTML.

It would be great if support for shortcodes could be added to Rmd files. I tried to use rmarkdown::md_document(preserve_yaml = TRUE) to first render Rmd to md, but < and > in the shortcode snippet would always get rewritten as < and > in the rendered md file

{{&lt; tweet 820477439050731521 &gt;}}

thereby thwarting Hugo.

Or is there perhaps a knitr option?

@yihui

This comment has been minimized.

Show comment
Hide comment
@yihui

yihui Jan 25, 2017

Member

I'm aware of this issue, and my current guess is that we will have to reinvent some shortcodes using the R syntax, otherwise you have to make a hard decision on whether to give up shortcodes or give up R in your post... It is not easy to have both since they have different Markdown rendering engines.

Member

yihui commented Jan 25, 2017

I'm aware of this issue, and my current guess is that we will have to reinvent some shortcodes using the R syntax, otherwise you have to make a hard decision on whether to give up shortcodes or give up R in your post... It is not easy to have both since they have different Markdown rendering engines.

@yihui

This comment has been minimized.

Show comment
Hide comment
@yihui

yihui Jan 25, 2017

Member

It just occurred to me that there could be a much easier way:

`r htmltools::HTML("{{< tweet 666616452582129664 >}}")`

This makes sure Pandoc does not touch the special characters< >. Oh, I feel so relieved after figuring out this trick!

impossible-parking

Member

yihui commented Jan 25, 2017

It just occurred to me that there could be a much easier way:

`r htmltools::HTML("{{< tweet 666616452582129664 >}}")`

This makes sure Pandoc does not touch the special characters< >. Oh, I feel so relieved after figuring out this trick!

impossible-parking

@lcolladotor

This comment has been minimized.

Show comment
Hide comment
@lcolladotor

lcolladotor Jan 25, 2017

Contributor

Awesome @yihui!

Great gif too hehe ^^

Contributor

lcolladotor commented Jan 25, 2017

Awesome @yihui!

Great gif too hehe ^^

@yihui

This comment has been minimized.

Show comment
Hide comment
@yihui

yihui Jan 25, 2017

Member

I'll provide a wrapper function in blogdown (something like `r blogdown::shortcode('tweet', '666616452582129664')`).

This problem was actually the most awkward drawback of using R Markdown on my mind when I started working on this project. I'm so glad that Hugo still processes shortcodes after I have converted (R) Markdown to HTML. Now I'm much more comfortable about R Markdown (other than TODO lists, pretty much Pandoc can do everything that Blackfriday can do).

Member

yihui commented Jan 25, 2017

I'll provide a wrapper function in blogdown (something like `r blogdown::shortcode('tweet', '666616452582129664')`).

This problem was actually the most awkward drawback of using R Markdown on my mind when I started working on this project. I'm so glad that Hugo still processes shortcodes after I have converted (R) Markdown to HTML. Now I'm much more comfortable about R Markdown (other than TODO lists, pretty much Pandoc can do everything that Blackfriday can do).

@mwhitaker

This comment has been minimized.

Show comment
Hide comment
@mwhitaker

mwhitaker Jan 25, 2017

Eureka! Rendering shortcodes in Rmd files has definitely eluded me and I spent more than two hours on this ;-)

The nice thing is that you could now use this approach to save the data output for example from an htmlwidget into a JSON file into the static folder, and then pull in via a shortcode in a Hugo template using the getJSON Hugo function. So you could just render from Rmd to md and let Hugo do the rest. That works very well and I find that approach very elegant.

The fact that this also works in HTML is great. Thank you!

mwhitaker commented Jan 25, 2017

Eureka! Rendering shortcodes in Rmd files has definitely eluded me and I spent more than two hours on this ;-)

The nice thing is that you could now use this approach to save the data output for example from an htmlwidget into a JSON file into the static folder, and then pull in via a shortcode in a Hugo template using the getJSON Hugo function. So you could just render from Rmd to md and let Hugo do the rest. That works very well and I find that approach very elegant.

The fact that this also works in HTML is great. Thank you!

@pssguy

This comment has been minimized.

Show comment
Hide comment
@pssguy

pssguy Jan 25, 2017

@mwhitaker Does that mean you could have a live twitter feed on a blogdown site?

pssguy commented Jan 25, 2017

@mwhitaker Does that mean you could have a live twitter feed on a blogdown site?

@mwhitaker

This comment has been minimized.

Show comment
Hide comment
@mwhitaker

mwhitaker Jan 25, 2017

@pssguy sure. Would be quite easy to do as long as the live twitter feed uses a Javascript API, which I think does. You won't need the Hugo getJSON function I mentioned earlier, just set up a shortcode.

mwhitaker commented Jan 25, 2017

@pssguy sure. Would be quite easy to do as long as the live twitter feed uses a Javascript API, which I think does. You won't need the Hugo getJSON function I mentioned earlier, just set up a shortcode.

@mwhitaker

This comment has been minimized.

Show comment
Hide comment
@mwhitaker

mwhitaker Jan 25, 2017

@yihui I have one follow-up question if you don't mind. I understand that with blogdown you take Rmd to HTML, which Hugo can still process.

Before your package I was trying to use rmarkdown::render("input.Rmd", rmarkdown::md_document(preserve_yaml = TRUE),...) in the hope that I could add htmlwidgets via shortcodes but I'd always get thwarted because of < and >.

Obviously it's great that I can use shortcodes in Rmd files using blogdown, so this might be a moot point now. Thanks again!

mwhitaker commented Jan 25, 2017

@yihui I have one follow-up question if you don't mind. I understand that with blogdown you take Rmd to HTML, which Hugo can still process.

Before your package I was trying to use rmarkdown::render("input.Rmd", rmarkdown::md_document(preserve_yaml = TRUE),...) in the hope that I could add htmlwidgets via shortcodes but I'd always get thwarted because of < and >.

Obviously it's great that I can use shortcodes in Rmd files using blogdown, so this might be a moot point now. Thanks again!

@lcolladotor

This comment has been minimized.

Show comment
Hide comment
@lcolladotor

lcolladotor Jan 27, 2017

Contributor

@mwhitaker if you want an .md file use knitr::render_markdown(TRUE). It'll work with the shortcodes.

You'll have to move it to hugo and all that manually. Plus make sure that the fig paths are properly set up.

Contributor

lcolladotor commented Jan 27, 2017

@mwhitaker if you want an .md file use knitr::render_markdown(TRUE). It'll work with the shortcodes.

You'll have to move it to hugo and all that manually. Plus make sure that the fig paths are properly set up.

@mwhitaker

This comment has been minimized.

Show comment
Hide comment
@mwhitaker

mwhitaker Jan 30, 2017

Thanks @lcolladotor but this is somehow not working for me. Maybe I am doing this wrong, but isn't knitr::render_markdown(TRUE) supposed to be used in a "setup" chunk in the Rmd file? I did that and then I used rmarkdown::render("input.Rmd", rmarkdown::md_document(preserve_yaml = TRUE),...) to render to .md. But I still get <> encoded.

mwhitaker commented Jan 30, 2017

Thanks @lcolladotor but this is somehow not working for me. Maybe I am doing this wrong, but isn't knitr::render_markdown(TRUE) supposed to be used in a "setup" chunk in the Rmd file? I did that and then I used rmarkdown::render("input.Rmd", rmarkdown::md_document(preserve_yaml = TRUE),...) to render to .md. But I still get <> encoded.

@lcolladotor

This comment has been minimized.

Show comment
Hide comment
@lcolladotor

lcolladotor Jan 30, 2017

Contributor

No, use it before using knitr::knit() to render the .Rmd file directly to get a .md file that still has the shortcode intact. See lcolladotor/markdown-redcarpet.tmbundle@f043c05#diff-2c37ba0c676929fe1064b758c2fccf15R39

Contributor

lcolladotor commented Jan 30, 2017

No, use it before using knitr::knit() to render the .Rmd file directly to get a .md file that still has the shortcode intact. See lcolladotor/markdown-redcarpet.tmbundle@f043c05#diff-2c37ba0c676929fe1064b758c2fccf15R39

@mwhitaker

This comment has been minimized.

Show comment
Hide comment
@mwhitaker

mwhitaker Jan 30, 2017

thank you @lcolladotor - works great!

mwhitaker commented Jan 30, 2017

thank you @lcolladotor - works great!

@yihui

This comment has been minimized.

Show comment
Hide comment
@yihui

yihui May 12, 2017

Member

FYI I have provided blogdown::shortcode() in the current dev version that you can use in R Markdown documents to write shortcodes. See its help page for more info.

Member

yihui commented May 12, 2017

FYI I have provided blogdown::shortcode() in the current dev version that you can use in R Markdown documents to write shortcodes. See its help page for more info.

@EllaKaye

This comment has been minimized.

Show comment
Hide comment
@EllaKaye

EllaKaye Jun 17, 2017

Hi @yihui. Thanks for this! Small point, but I think there's a typo in the example on the help page for this. It gives shortcode("tweet", 1234567), but when I tried that (with an appropriate tweet ID), it didn't work, but putting the ID in as a string does.

EllaKaye commented Jun 17, 2017

Hi @yihui. Thanks for this! Small point, but I think there's a typo in the example on the help page for this. It gives shortcode("tweet", 1234567), but when I tried that (with an appropriate tweet ID), it didn't work, but putting the ID in as a string does.

yihui added a commit that referenced this issue Jun 17, 2017

@yihui

This comment has been minimized.

Show comment
Hide comment
@yihui

yihui Jun 17, 2017

Member

@EllaKaye Thanks! You are correct that the tweet ID should be quoted.

Member

yihui commented Jun 17, 2017

@EllaKaye Thanks! You are correct that the tweet ID should be quoted.

@pssguy

This comment has been minimized.

Show comment
Hide comment
@pssguy

pssguy Jul 23, 2017

Sorry to be a pain, but I'm missing something pretty basic here
The html output of an Rmarkdown document includes this

shortcodeissue

This seems to tally with the documentation

"Value
A character string wrapped in htmltools::HTML(); shortcode() returns a string of the form {{% name args %}},"

but I want to see the actual tweet

pssguy commented Jul 23, 2017

Sorry to be a pain, but I'm missing something pretty basic here
The html output of an Rmarkdown document includes this

shortcodeissue

This seems to tally with the documentation

"Value
A character string wrapped in htmltools::HTML(); shortcode() returns a string of the form {{% name args %}},"

but I want to see the actual tweet

@yihui

This comment has been minimized.

Show comment
Hide comment
@yihui

yihui Jul 24, 2017

Member

@pssguy Could you provide a reproducible example? shortcode() is a function specific to blogdown and Hugo. You cannot use it elsewhere.

Member

yihui commented Jul 24, 2017

@pssguy Could you provide a reproducible example? shortcode() is a function specific to blogdown and Hugo. You cannot use it elsewhere.

@pssguy

This comment has been minimized.

Show comment
Hide comment
@pssguy

pssguy Jul 24, 2017

False Alarm, I think
It was after knitting that single page - so guess Hugo would not be involved, presumably
With serve_site() it worked OK - though on one occasion testing I had to refresh the browser

pssguy commented Jul 24, 2017

False Alarm, I think
It was after knitting that single page - so guess Hugo would not be involved, presumably
With serve_site() it worked OK - though on one occasion testing I had to refresh the browser

@yihui

This comment has been minimized.

Show comment
Hide comment
@yihui

yihui Jul 24, 2017

Member

The documentation says (https://bookdown.org/yihui/blogdown/workflow.html):

Do not click the Knit button on the RStudio toolbar.

Member

yihui commented Jul 24, 2017

The documentation says (https://bookdown.org/yihui/blogdown/workflow.html):

Do not click the Knit button on the RStudio toolbar.

@KevCaz KevCaz referenced this issue Feb 4, 2018

Closed

Trick and tips #42

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment