arvi1000
arvi1000

Reputation: 9582

Avoid auto-hyperlink generation with knitr, markdown

knitr automatically generates links when knitting .Rmd to .html. Very often that's useful, but just now it's not what I want.

Suppose you have this .Rmd file:

---
title: "Doc title"
output: html_document
---

I don't want [email protected], but it comes out that way. 

If you wrap it in an R expression  `r "[email protected]"`.

Is there some kind of CSS trick I can avail myself of if I wanted <style='nolink'>www.something.com</style> not to be a link?

Knitting like this:

library(knitr)
knit2html('that_file.Rmd', 'that_file.html')

results in all those things being links.

enter image description here

Is there an easy way to generally keep the auto-link generation, but disable it selectively in particular lines? Thanks for any thoughts.

Edit: i guess I should have actually tried the solution below before accepting. This in the .Rmd

I don't want this <!-- breaklink -->@to-be-a-link.com

...doesnt actually parse to an HTML comment, because the -- gets changed to an em dash (by knitr? pandoc?) and then I get:

enter image description here

Upvotes: 10

Views: 3578

Answers (3)

tarleb
tarleb

Reputation: 22609

The easiest and most localized way to do this is to "backslash-escape" the at sign:

I don't want this\@to-be-a-link.com, and now it isn't. 

This also works with weblinks:

Not a link: https\://posit.co

The autolink feature can also be completely disabled by turning off the autolink_bare_uris extension:

---
title: "Doc title"
output:
  html_document:
    md_extensions: -autolink_bare_uris
---

I don't want [email protected], and now it isn't.

URLs must then be wrapped in angular brackets to get a link: <https://posit.co>.

See also the "HTML document" section in the R Markdown Cookbook.

Upvotes: 1

Carl Boneri
Carl Boneri

Reputation: 2722

Very late to this party, however there's another way to accomplish this using html entities.

Using an entity rather than the normal charater-mark will essentially escape whatever and when rendered will not become a hyperlink.

For example, if I wanted a url to be in plaintext(meaning i dont want it to look like code , which for most cases would work just fine), but i had a usecase where I wanted part of the url to be bold, so:

In my .Rmd file:

Hyperlinked  
: https://sub-domain-**env**.domain.com/path/to/collection
Non-Hyperlinked  
: https&#58;//sub-domain-**env**.domain.com/path/to/collection

enter image description here

Upvotes: 0

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

Reputation: 193517

Two options that I see are (1) using bare backticks, or (2) "breaking" the link by using an empty HTML comment.

Example:

---
title: "Doc title"
output: html_document
---

I don't want this<!-- -->@to-be-a-link.com, but it comes out that way. 

If you wrap it in an R expression `[email protected]`.

Is there some kind of CSS trick I can avail myself of if I wanted 
<style='nolink'>http<!-- -->://www.something.com</style> not to 
be a link?

Becomes:

enter image description here

Upvotes: 8

Related Questions