From d82dca472104ff56d8f8182c2acf46ff59720268 Mon Sep 17 00:00:00 2001
From: Ultroncn <82553854+Ultroncn@users.noreply.github.com>
Date: Tue, 17 Nov 2026 21:04:22 +0000
Subject: [PATCH 1/2] fix: require a space after ATX hashes; parse --- after
headings and indented code
- #64: '#sth' no longer parses as a heading. ATX headings now
require a space or tab after the hashes, as in CommonMark, so
hashtags and similar text stay literal. A hash run followed by a
newline no longer swallows the next line as heading text either.
- #103, #112: a '---' thematic break after an ATX heading or an
indented code block now parses as
. Both block types swallow
the blank line that follows them, which used to leave the rule
without the blank line the HR pattern requires; the HR pattern now
also matches a rule at the start of a line when the preceding
newlines were already consumed. Setext headings ('foo' + '---')
are unaffected, and a rule at the start or end of a document now
parses too.
---
src/index.js | 2 +-
test/index.js | 26 ++++++++++++++++++++++++++
2 files changed, 27 insertions(+), 1 deletion(-)
diff --git a/src/index.js b/src/index.js
index 3938c5c..820ec70 100644
--- a/src/index.js
+++ b/src/index.js
@@ -24,7 +24,7 @@ function encodeAttr(str) {
/** Parse Markdown into an HTML String. */
export default function parse(md, prevLinks) {
- let tokenizer = /((?:^|\n+)(?:\n---+|\* \*(?: \*)+)\n)|(?:^``` *(\w*)\n([\s\S]*?)\n```$)|((?:(?:^|\n+)(?:\t| {2,}).+)+\n*)|((?:(?:^|\n)([>*+-]|\d+\.)\s+.*)+)|(?:!\[([^\]]*?)\]\(([^)]+?)\))|(\[)|(\](?:\(([^)]+?)\))?)|(?:(?:^|\n+)([^\s].*)\n(-{3,}|={3,})(?:\n+|$))|(?:(?:^|\n+)(#{1,6})\s*(.+)(?:\n+|$))|(?:`([^`].*?)`)|( \n\n*|\n{2,}|__|\*\*|[_*]|~~)/gm,
+ let tokenizer = /((?:^|\n+)(?:\n---+|\* \*(?: \*)+)\n|^---+(?:\n|$))|(?:^``` *(\w*)\n([\s\S]*?)\n```$)|((?:(?:^|\n+)(?:\t| {2,}).+)+\n*)|((?:(?:^|\n)([>*+-]|\d+\.)\s+.*)+)|(?:!\[([^\]]*?)\]\(([^)]+?)\))|(\[)|(\](?:\(([^)]+?)\))?)|(?:(?:^|\n+)([^\s].*)\n(-{3,}|={3,})(?:\n+|$))|(?:(?:^|\n+)(#{1,6})[ \t]+(.+)(?:\n+|$))|(?:`([^`].*?)`)|( \n\n*|\n{2,}|__|\*\*|[_*]|~~)/gm,
context = [],
out = '',
links = prevLinks || {},
diff --git a/test/index.js b/test/index.js
index 5262b16..a3974df 100644
--- a/test/index.js
+++ b/test/index.js
@@ -37,6 +37,16 @@ describe('snarkdown()', () => {
expect(snarkdown('### I like tiny libraries')).to.equal('I like tiny libraries
');
});
+ it('requires a space after the hashes', () => {
+ // #64: "#sth" is plain text (e.g. a hashtag), not a heading.
+ expect(snarkdown('#sth')).to.equal('#sth');
+ expect(snarkdown('##sth')).to.equal('##sth');
+ expect(snarkdown('####### seven')).to.equal('####### seven');
+ expect(snarkdown('#\nfoo')).to.equal('#\nfoo');
+ // a tab counts as the required whitespace, too:
+ expect(snarkdown('#\ttitle')).to.equal('title
');
+ });
+
it('parses titles with reference links', () => {
expect(
snarkdown('# I like [tiny libraries]\n\n[tiny libraries]: https://github.com/developit/snarkdown')
@@ -144,6 +154,22 @@ describe('snarkdown()', () => {
expect(snarkdown('> foo\n\n---\nbar')).to.equal('foo
bar');
});
+ it('should parse --- after a heading', () => {
+ // #112: the heading swallows the blank line that follows it,
+ // which used to leave the rule without the blank line it needs.
+ expect(snarkdown('### Title\n\n---\n\ntext')).to.equal('Title
\ntext');
+ expect(snarkdown('---\n\nhello'), 'at the start of a document').to.equal('
\nhello');
+ expect(snarkdown('foo\n\n---'), 'at the end of a document').to.equal('foo
');
+ });
+
+ it('should parse --- after an indented code block', () => {
+ // #103: same cause as above, with an indented code block
+ // swallowing the blank line before the rule.
+ expect(
+ snarkdown('Text into\n\n---\n\nBut, not this\n\n Sample Code Block\n\n---\n\ntest')
+ ).to.equal('Text into
\nBut, not thisSample Code Block
\ntest');
+ });
+
it('should parse * * *', () => {
expect(snarkdown('foo\n* * *\nbar')).to.equal('foo
bar');
expect(snarkdown('foo\n* * * *\nbar'), '* * * *').to.equal('foo
bar');
From 4e9312af9858363cdd7bb06b8f3e30d28901ed66 Mon Sep 17 00:00:00 2001
From: Ultron <82553854+Ultroncn@users.noreply.github.com>
Date: Thu, 19 Nov 2026 23:31:39 +0800
Subject: [PATCH 2/2] fix: keep empty and dash-only ATX headings intact
---
src/index.js | 6 ++++--
test/index.js | 9 +++++++++
2 files changed, 13 insertions(+), 2 deletions(-)
diff --git a/src/index.js b/src/index.js
index 820ec70..b77d50d 100644
--- a/src/index.js
+++ b/src/index.js
@@ -24,7 +24,7 @@ function encodeAttr(str) {
/** Parse Markdown into an HTML String. */
export default function parse(md, prevLinks) {
- let tokenizer = /((?:^|\n+)(?:\n---+|\* \*(?: \*)+)\n|^---+(?:\n|$))|(?:^``` *(\w*)\n([\s\S]*?)\n```$)|((?:(?:^|\n+)(?:\t| {2,}).+)+\n*)|((?:(?:^|\n)([>*+-]|\d+\.)\s+.*)+)|(?:!\[([^\]]*?)\]\(([^)]+?)\))|(\[)|(\](?:\(([^)]+?)\))?)|(?:(?:^|\n+)([^\s].*)\n(-{3,}|={3,})(?:\n+|$))|(?:(?:^|\n+)(#{1,6})[ \t]+(.+)(?:\n+|$))|(?:`([^`].*?)`)|( \n\n*|\n{2,}|__|\*\*|[_*]|~~)/gm,
+ let tokenizer = /((?:^|\n+)(?:\n---+|\* \*(?: \*)+)\n|^---+(?:\n|$))|(?:^``` *(\w*)\n([\s\S]*?)\n```$)|((?:(?:^|\n+)(?:\t| {2,}).+)+\n*)|((?:(?:^|\n)([>*+-]|\d+\.)\s+.*)+)|(?:!\[([^\]]*?)\]\(([^)]+?)\))|(\[)|(\](?:\(([^)]+?)\))?)|(?:(?:^|\n+)([^\s].*)\n(-{3,}|={3,})(?:\n+|$))|(?:(?:^|\n+)(#{1,6})[ \t]+(.*)(?:\n+|$))|(?:`([^`].*?)`)|( \n\n*|\n{2,}|__|\*\*|[_*]|~~)/gm,
context = [],
out = '',
links = prevLinks || {},
@@ -91,7 +91,9 @@ export default function parse(md, prevLinks) {
// Headings:
else if (token[12] || token[14]) {
t = 'h' + (token[14] ? token[14].length : (token[13]>'=' ? 1 : 2));
- chunk = '<'+t+'>' + parse(token[12] || token[15], links) + ''+t+'>';
+ let title = token[12] || token[15];
+ // A line of dashes is literal inside a heading.
+ chunk = '<'+t+'>' + (/^---+$/.test(title) ? title : parse(title, links)) + ''+t+'>';
}
// `code`:
else if (token[16]) {
diff --git a/test/index.js b/test/index.js
index a3974df..d780ce2 100644
--- a/test/index.js
+++ b/test/index.js
@@ -47,6 +47,15 @@ describe('snarkdown()', () => {
expect(snarkdown('#\ttitle')).to.equal('title
');
});
+ it('allows empty ATX titles after a space or tab', () => {
+ expect(snarkdown('# ')).to.equal('');
+ expect(snarkdown('##\t')).to.equal('');
+ });
+
+ it('keeps dashes in a title as text', () => {
+ expect(snarkdown('# ---')).to.equal('---
');
+ });
+
it('parses titles with reference links', () => {
expect(
snarkdown('# I like [tiny libraries]\n\n[tiny libraries]: https://github.com/developit/snarkdown')