rstudio / htmltools

Tools for HTML generation and output
https://rstudio.github.io/htmltools/
214 stars 67 forks source link

includeMarkdown doesnot render the `<id>` of the headers. #380

Closed ShixiangWang closed 1 year ago

ShixiangWang commented 1 year ago

I want to create a TOC for a markdown used in the shiny. However, the <id> is not generated. I followed the markdown grammar to add the <id> manually not work.

> includeMarkdown(set_md_path("usage.md"))

....

<h2>7. Global Setting Page {#7-global-setting-page}</h2>
<p>This page provides global options for controlling access to underlying data. By setting default options, we try to minimize the user’s initial learning cost and ensure that the results displayed on the website are biologically meaningful. For example, we only analyze sample data related to immunotherapy by default, in order to match the core goals of our website, and to remove data bias (confusion caused by different treatments in the data) that first-time users may not see. In addition, we have set up default configuration for CircRNA expression using appropriate CPM quantification. Users can adjust these global options according to their own needs after becoming familiar with the website, in order to facilitate exploring more possibilities of this website.</p>
<img src="https://cdn.jsdelivr.net/gh/shixiangwang/cdn/img/7.PNG" alt="snapshot of Setting Page" style="zoom:50%;" />

You can see that

## 7. Global Setting Page {#7-global-setting-page}

is rendered as

<h2>7. Global Setting Page {#7-global-setting-page}</h2>

Not the same as https://www.markdownguide.org/extended-syntax/#heading-ids.

So what should I do to achieve this?

Best,

Shixiang

gadenbuie commented 1 year ago

Which versions of the htmltools, markdown and commonmark packages are you using? For me, with the latest of each of those I do see attributes on headings being supported

library(htmltools)

tmp_path <- tempfile(fileext = ".md")
writeLines(c(
  "## Hello Markdown {#hello}",
  "",
  "Just some regular text here."
), tmp_path)

includeMarkdown(tmp_path) |> print(browse = FALSE)
#> <h2 id="hello">Hello Markdown</h2>
#> <p>Just some regular text here.</p>

It's most likely that your version of markdown is out of date. That package was recently updated to use commonmark for markdown processing. Previously, I don't think attributes on headings would have worked.

Here are my session details to compare with your current setup.

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.3.0 (2023-04-21) #> os macOS Ventura 13.4 #> system aarch64, darwin20 #> ui X11 #> language (EN) #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz America/New_York #> date 2023-06-21 #> pandoc 3.1.3 @ /opt/homebrew/bin/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> cli 3.6.1 2023-03-23 [1] CRAN (R 4.3.0) #> commonmark 1.9.0 2023-03-17 [1] CRAN (R 4.3.0) #> digest 0.6.31 2022-12-11 [1] CRAN (R 4.3.0) #> evaluate 0.21 2023-05-05 [1] CRAN (R 4.3.0) #> fastmap 1.1.1 2023-02-24 [1] CRAN (R 4.3.0) #> fs 1.6.2 2023-04-25 [1] CRAN (R 4.3.0) #> glue 1.6.2 2022-02-24 [1] CRAN (R 4.3.0) #> htmltools * 0.5.5 2023-03-23 [1] CRAN (R 4.3.0) #> knitr 1.43 2023-05-25 [1] CRAN (R 4.3.0) #> lifecycle 1.0.3 2022-10-07 [1] CRAN (R 4.3.0) #> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.3.0) #> markdown 1.7 2023-05-16 [1] CRAN (R 4.3.0) #> reprex 2.0.2 2022-08-17 [1] CRAN (R 4.3.0) #> rlang 1.1.1 2023-04-28 [1] CRAN (R 4.3.0) #> rmarkdown 2.22 2023-06-01 [1] CRAN (R 4.3.0) #> sessioninfo 1.2.2 2021-12-06 [2] CRAN (R 4.3.0) #> withr 2.5.0 2022-03-03 [1] CRAN (R 4.3.0) #> xfun 0.39 2023-04-20 [1] CRAN (R 4.3.0) #> yaml 2.3.7 2023-01-23 [1] CRAN (R 4.3.0) #> #> [1] /Users/garrick/R-dev #> [2] /Users/garrick/Library/R/arm64/4.3/library #> [3] /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library #> #> ────────────────────────────────────────────────────────────────────────────── ```
ShixiangWang commented 1 year ago

Hi @gadenbuie , thank you. I will update and try it again.

ShixiangWang commented 1 year ago

Fixed. Thanks again.

gadenbuie commented 1 year ago

@ShixiangWang You're welcome! If you run into something similar or are curious about the syntax supported by markdown (and therefore includeMarkdown()), the introduction vignette is a great resource.

ShixiangWang commented 1 year ago

@gadenbuie Got it!