Skip to content
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
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep horizontal-rule recognition out of heading content

For a heading whose text consists of dashes, such as # ---, the new HR alternative also matches during the recursive parse() call that renders the heading's contents. Consequently, this input now produces <h1><hr /></h1> instead of the previous <h1>---</h1>, removing the title text and inserting a block element inside the heading. Restrict the new HR recognition to block contexts so that heading contents remain inline text.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked this and the empty-heading finding against the base and PR commits. # --- renders as <h1>---</h1> on the base and <h1><hr /></h1> on this PR. Empty ATX headings followed by a space or tab also render as literal text on the PR.

Would you be open to a small follow-up on this PR covering both cases, with focused regression tests? I'd appreciate your guidance on the scope.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Allow empty ATX text after the required separator

For an empty heading at the end of a document, such as # or ##\t, the required separator consumes the only whitespace and (.+) then cannot match. These inputs were rendered as headings before this change, but now emit literal hash characters, even though they satisfy the new separator requirement and are valid empty ATX headings. Allow the content capture to be empty while retaining the mandatory space or tab.

Useful? React with 👍 / 👎.

context = [],
out = '',
links = prevLinks || {},
Expand Down
26 changes: 26 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ describe('snarkdown()', () => {
expect(snarkdown('### I like tiny libraries')).to.equal('<h3>I like tiny libraries</h3>');
});

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('<h1>title</h1>');
});

it('parses titles with reference links', () => {
expect(
snarkdown('# I like [tiny libraries]\n\n[tiny libraries]: https://github.com/developit/snarkdown')
Expand Down Expand Up @@ -144,6 +154,22 @@ describe('snarkdown()', () => {
expect(snarkdown('> foo\n\n---\nbar')).to.equal('<blockquote>foo</blockquote><hr />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('<h3>Title</h3><hr />\ntext');
expect(snarkdown('---\n\nhello'), 'at the start of a document').to.equal('<hr />\nhello');
expect(snarkdown('foo\n\n---'), 'at the end of a document').to.equal('foo<br /><hr />');
});

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<hr />\nBut, not this<pre class="code poetry"><code>Sample Code Block</code></pre><hr />\ntest');
});

it('should parse * * *', () => {
expect(snarkdown('foo\n* * *\nbar')).to.equal('foo<hr />bar');
expect(snarkdown('foo\n* * * *\nbar'), '* * * *').to.equal('foo<hr />bar');
Expand Down