emonigma
emonigma

Reputation: 4406

Can Pandoc mark text inside a code block?

I use <mark>...</mark> to highlight text in a Markdown document, such as the Google Python Style Guide. I am having difficulty higlighting code. For example, I want to highlight how to annotate with types:

```python
<mark>def func(a: int) -> List[int]:</mark>
```

but this pandoc command:

pandoc -s -t html5 -o "Google Python style guide.html" "Google Python style guide.md"

shows <mark> and </mark> as code and does not render an HTML highlight.

One solution is to use a pre tag, such as:

<pre><mark>def func(a: int) -> List[int]:</mark></pre>

which does render an HTML highlight.

Can Pandoc render the HTML higlights without having to convert all ```python blocks to pre tags?

Upvotes: 3

Views: 3003

Answers (2)

mb21
mb21

Reputation: 39313

No, not by default. How is pandoc supposed to know the <mark> is not part of your code?

However, you can write a pandoc filter that matches on each code block and converts it to a raw html block. Something like (untested):

function CodeBlock(elem)
  html = "<pre>" .. elem.text .. "</pre>"
  return pandoc.RawBlock("html", html)
end

Note that you'll have to make sure you don't have other unescaped HTML in your code blocks.

Update

If you also want syntax highlighting, probably you want to try the pandoc-emphasize-code filter.

Upvotes: 3

tarleb
tarleb

Reputation: 22609

If you will always mark whole lines, then you can use CSS to do so.

The code block should be given an ID to make it easier to target it

``` {#types-demo .python}
def func(a: int) -> List[int]:
   return [a]
```

To highlight the first line, include this in your document:

```{=html}
<style>
#types-demo-1 {
    background-color: #ff0;
} 
</style>
```

Syntax highlighting should continue to work.

Upvotes: 1

Related Questions