From 3dabaffd6b30ed0fe72be1f82a199fb6464b56ad Mon Sep 17 00:00:00 2001 From: ee7 <45465154+ee7@users.noreply.github.com> Date: Wed, 22 Mar 2023 22:17:56 +0100 Subject: [PATCH] generate: don't move link reference definition out of block (#738) Closes: #737 --- src/generate/generate.nim | 3 ++- tests/test_generate.nim | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/generate/generate.nim b/src/generate/generate.nim index 2d9c018d..6f78bafd 100644 --- a/src/generate/generate.nim +++ b/src/generate/generate.nim @@ -32,7 +32,8 @@ func alterHeadings(s: string, linkDefs: var seq[string], h2 = ""): string = if s.continuesWith("#", i+1) and not (inFencedCodeBlock or inFencedCodeBlockTildes or inCommentBlock): result.add '#' - elif s.continuesWith("[", i+1): + elif s.continuesWith("[", i+1) and not (inFencedCodeBlock or + inFencedCodeBlockTildes or inCommentBlock): let j = s.find("]:", i+2) if j > i+2 and j < s.find('\n', i+2): var line = "" diff --git a/tests/test_generate.nim b/tests/test_generate.nim index 129319e1..9b42363a 100644 --- a/tests/test_generate.nim +++ b/tests/test_generate.nim @@ -74,6 +74,28 @@ proc testGenerate = check alterHeadings(s, linkDefs, "Maps") == "## Maps\n\n" & expected check linkDefs.len == 0 + test "alterHeadings: keeps link reference definition inside block": + const s = """ + # Heading 1 + + ~~~~exercism/note + See the [foo docs][foo-docs] for more details. + + [foo-docs]: https://example.com + ~~~~ + """.unindent() + + const expected = """ + ~~~~exercism/note + See the [foo docs][foo-docs] for more details. + + [foo-docs]: https://example.com + ~~~~""".unindent() + + var linkDefs = newSeq[string]() + check alterHeadings(s, linkDefs) == expected + check linkDefs.len == 0 + proc main = testGenerate()