I have a character vector I want include as body text for an html document I'm making in Quarto. I want some of the strings to be identified as headers and be included in the table of contents. I'm running into a few problems.
If I identify the headers in html, they will follow the given heading level style, but they won't be included in table of contents. So they have to be identified with markdown, got it. Two issues with this. First, if I with a single string, it works fine. But I can't figure out how to have the markdown identified when it's more than one string. Concatenating doesn't work, understandably. But without that I get commas in between everything. I've also tried using a for loop to print individually, with either a list or vector, but then I get indices.
The second issue is that some of the strings that aren't headers, but just regular body text, start with (a), (1), (A), etc. When rendered, these get translated into ordered lists that never end, and the paragraphs keep getting pushed to the right. I wrapped these in <p></p>
(primarily to add varying css classes to them, which works well with external stylesheet) and they still render as unending lists unless I put them in an html code block. But I need to print the headings outside of html code blocks so they will be interpreted as markdown, assuming I can fix that problem.
Any suggestions on if/how I could make this work?
Example code:
---
title: "example"
format:
html:
toc: true
---
```{r}
html_text <- c("<h2>this is an html heading</h2>", "<p>some text here</p>", "<p class='class1'>(a) some text here</p>", "<p>(b) some text here</p>", "<p class='class2'>(1) some text here</p>") |>
paste(collapse = " ")
md_text <- c("### this is a markdown heading", "<p>some text here</p>", "<p class='class1'>(a) some text here</p>", "<p>(b) some text here</p>", "<p class='class2'>(1) some text here</p>")
md_heading <- md_text[1]
md_text2 <- paste(md_text, collapse = " ")
```
`r html_text`
```{=html}
`r html_text`
```
`r md_heading`
`r md_text2`
```{r results='asis'}
## print individually as is
for (i in md_text) {
print(i)
}
```
I think you have sorted out your first issue almost, simply use paste0
to combine the body texts and then use inline R-code.
Now to solve the second issue you need to disable parsing (a)
, (A)
or (1)
as pandoc markdown list and to do that simply add backslashes before them.
---
title: "example"
format:
html:
toc: true
---
```{r}
md_text <- c("### this is a markdown heading\n\n",
"<p>some text here</p>",
"<p class='class1'>\\(a) some text here</p>",
"<p>\\(b) some text here</p>",
"<p class='class2'>\\(1) some text here</p>")
md_text2 <- paste0(md_text, collapse="")
```
`r md_text2`