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

Add commonmark-code-blocks filter for producing CommonMark-suggested HTML for fenced code blocks #224

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions commonmark-code-blocks/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
DIFF := diff --ignore-all-space --ignore-blank-lines -u
PANDOC ?= pandoc

.PHONY: test
test:
@$(PANDOC) --lua-filter=commonmark-code-blocks.lua -f commonmark -t html sample.md | \
$(DIFF) expected.html -

.PHONY: all
all: test

23 changes: 23 additions & 0 deletions commonmark-code-blocks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# commonmark-code-blocks

This filter produces code blocks with language CSS class format
suggested by the CommonMark specification ("language-$lang").

The CommonMark spec suggests that converting ````somelang` to HTML
should produce `<code class="language-somelang">`,
as described in [example 141](https://spec.commonmark.org/0.30/#example-141).

That's what most CommonMark implementations do.
Pandoc, however, outputs `<code class="somelang">` instead.

That can be a problem for syntax highlighters that expect
`language-*` classes and use them to determine the language.

This filter takes over the code block rendering process
to produce CommonMark-style output.

## Usage

This filter does not require any setup or external tools.
Just run pandoc with `--lua-filter=commonmark-code-blocks.lua`.

59 changes: 59 additions & 0 deletions commonmark-code-blocks/commonmark-code-blocks.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
--[[

commonmark-code-blocks -- produces code blocks with language CSS class format
suggested by the CommonMark specification ("language-$lang").

MIT License

Copyright (c) 2021 Daniil Baturin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
]]

--[[

The CommonMark spec suggests that
"```somelang" should produce <code class="language-somelang">

That's what most CommonMark implementations do.

Pandoc, however, outputs <code class="somelang">

That can be a problem for syntax highlighters that expect
"language-*" classes and use them to determine the language.

This filter takes over the code block rendering process
to produce CommonMark-style output.

]]

function CodeBlock(block)
if FORMAT:match 'html' then
local lang_attr = ""
if (#block.classes > 0) then
lang_attr = string.format("class=\"language-%s\"", block.classes[1])
else
-- Ignore code blocks where language is not specified
end

local code = block.text:gsub("&", "&amp;"):gsub("<", "&lt;"):gsub(">", "&gt;")

local html = string.format('<pre><code %s>%s</code></pre>', lang_attr, code)
return pandoc.RawBlock('html', html)
end
end

2 changes: 2 additions & 0 deletions commonmark-code-blocks/expected.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<pre><code class="language-basic">10 PRINT "HELLO WORLD"</code></pre>

3 changes: 3 additions & 0 deletions commonmark-code-blocks/sample.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```basic
10 PRINT "HELLO WORLD"
```