Skip to content

Commit

Permalink
generate: support HTML comment block
Browse files Browse the repository at this point in the history
This is only one of many possible HTML blocks [1], but probably the most
important one.

[1] https://spec.commonmark.org/0.30/#html-blocks
  • Loading branch information
ee7 committed Jun 22, 2022
1 parent 118fd27 commit da1d695
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
9 changes: 7 additions & 2 deletions src/generate/generate.nim
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,19 @@ func alterHeaders(s: string, title: string): string =
result.add &"## {title}"
# Demote other headers
var inFencedCodeBlock = false
var inCommentBlock = false
while i < s.len:
result.add s[i]
if s[i] == '\n':
# When inside a fenced code block, don't alter a line that begins with '#'
if s.continuesWith("#", i+1) and not inFencedCodeBlock:
# Add a '#' to a line that begins with '#', unless inside a code or HTML block.
if s.continuesWith("#", i+1) and not (inFencedCodeBlock or inCommentBlock):
result.add '#'
elif s.continuesWith("```", i+1):
inFencedCodeBlock = not inFencedCodeBlock
elif s.continuesWith("<!--", i+1):
inCommentBlock = true
elif inCommentBlock and s.continuesWith("-->", i):
inCommentBlock = false
inc i
strip result

Expand Down
8 changes: 6 additions & 2 deletions tests/test_generate.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ proc testGenerate =
The quick brown fox jumps over a lazy dog.
The five boxing wizards jump quickly.
<!--
# This line is not a header
This line is in an HTML comment block -->
### Header 3
Expand Down Expand Up @@ -44,7 +46,9 @@ proc testGenerate =
The quick brown fox jumps over a lazy dog.
The five boxing wizards jump quickly.
<!--
# This line is not a header
This line is in an HTML comment block -->
#### Header 3
Expand Down

0 comments on commit da1d695

Please sign in to comment.