Skip to content
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 anchored/reference links #47

Merged
merged 15 commits into from
May 26, 2021
Merged

Support anchored/reference links #47

merged 15 commits into from
May 26, 2021

Conversation

zkamvar
Copy link
Member

@zkamvar zkamvar commented May 25, 2021

Summary

This will allow anchored links in the form of [text][rel-link] and
[rel-link]: <URL> "Title" to be processed with the folowing caveats:

  1. The URL is assumed to have no spaces
  2. The anchor will sink to the bottom of the document (until we find a
    good process for re-introducing text nodes into the document).

This will fix #45

Example

Without anchor link processing

library("tinkr")
f <- system.file("extdata", "link-test.md", package = "tinkr")
m <- yarn$new(f, anchor_links = FALSE)
m$tail(30) # anchor links unprocesed
#> ---
#> title: this tests links
#> ---
#> 
#> ## These are some links that are valid in basic markdown
#> 
#> This is some text [that contains links](https://example.com/1) which
#> [can be `inline`](https://example.com/2) or [can be spread across multiple lines
#> because the link text is JUST TOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
#> LONG, y'all](https://example.com/3).
#> 
#> Mainly, we want to see if [link text
#> by reference](https://example.com/4) and if links [can be referenced multiple times](https://example.com/1)
#> 
#> This should also [include non-reference links](https://example.com/5)
#> 
#> If you write \[some link text\]\[link2\], that will appear as [some link text](https://example.com/2 "link with title!")
#> 
#> ## This is some extended markdown content {#extended .callout}
#> 
#> This should also include references that use [standalone](https://example.com/standalone) links and
#> footnotes should not be properly parsed and will be considered 'asis' nodes,
#> at least that's what I *believe*\[^footy\]. Maybe this might not pan out \[^but who
#> knows? footnotes are **WEIRD**, man\].
#> 
#> <!-- links go here! -->
#> 
#> \[^footy\]: this is a footnote that
#> should be preserved

With Anchor link processing

m <- yarn$new(f)
m$tail(30) # anchor links now exist!
#> [can be `inline`][link2] or [can be spread across multiple lines
#> because the link text is JUST TOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
#> LONG, y'all][link3].
#> 
#> Mainly, we want to see if [link text
#> by reference][link4] and if links [can be referenced multiple times][this fun link1]
#> 
#> This should also [include non-reference links](https://example.com/5)
#> 
#> If you write \[some link text\]\[link2\], that will appear as [some link text][link2]
#> 
#> ## This is some extended markdown content {#extended .callout}
#> 
#> This should also include references that use [standalone][standalone] links and
#> footnotes should not be properly parsed and will be considered 'asis' nodes,
#> at least that's what I *believe*\[^footy\]. Maybe this might not pan out \[^but who
#> knows? footnotes are **WEIRD**, man\].
#> 
#> <!-- links go here! -->
#> 
#> \[^footy\]: this is a footnote that
#> should be preserved
#> 
#> [this fun link1]: https://example.com/1
#> [link2]: https://example.com/2 "link with title!"
#> [link3]: https://example.com/3
#> [link4]: https://example.com/4
#> [standalone]: https://example.com/standalone

Created on 2021-05-24 by the reprex package (v2.0.0)

resolve_links now works

TODO:

- add tests
- integrate into `to_xml()` with tryCatch() to avoid read errors
- add documentation information
This will allow anchored links in the form of `[text][rel-link]` and
[rel-link]: <URL> "Title" to be processed with the folowing caveats:

1. The URL is assumed to have no spaces
2. The anchor will sink to the bottom of the document (until we find a
   good process for re-introducing text nodes into the document).
R/add_md.R Outdated Show resolved Hide resolved
R/add_md.R Outdated Show resolved Hide resolved
Copy link
Member

@maelle maelle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Brilliant as usual 🌟

R/asis-nodes.R Show resolved Hide resolved
protect_footnotes <- function(body, ns = md_ns()) {
body <- copy_xml(body)

feet <- footnote_check(body, ns)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙂 🦶

R/asis-nodes.R Show resolved Hide resolved
R/resolve-links.R Show resolved Hide resolved
R/resolve-links.R Outdated Show resolved Hide resolved
R/resolve-links.R Outdated Show resolved Hide resolved
R/resolve-links.R Show resolved Hide resolved
R/to_xml.R Outdated Show resolved Hide resolved
README.Rmd Outdated Show resolved Hide resolved
zkamvar and others added 8 commits May 25, 2021 08:19
Co-authored-by: Maëlle Salmon <maelle.salmon@yahoo.se>
I forgot to add an anchor to the link search, so a.com/ and a.com/b
would both match a.com/, which resulted in an error.

This also means that links with the same URLs and different titles will
no longer match.
Sometimes you want to demonstrate links in markdown documents by
protecting them with code blocks. This unfortunately creates a situation
where a link matches twice and you need to choose. Since these links are
not present in the XML in the first place, it's safe to choose a single
link of the two that has sunk the furthest.
@zkamvar
Copy link
Member Author

zkamvar commented May 26, 2021

After a few false starts, I have run this against the suite of Software Carpentry lessons without error and I think I should be able to merge this now!

@zkamvar zkamvar merged commit 5d99041 into master May 26, 2021
@zkamvar zkamvar deleted the resolve-link branch May 26, 2021 18:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add option to preserve reference links
2 participants