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
34 changes: 22 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ function encodeAttr(str) {
return (str+'').replace(/"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
}

/** Parse Markdown into an HTML String. */
export default function parse(md, prevLinks) {
/** Parse Markdown into an HTML String.
* @param {boolean} [inline] only parse inline formatting, no block constructs
* (used for heading contents, which can't contain blocks)
*/
export default function parse(md, prevLinks, inline) {
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,
context = [],
out = '',
Expand Down Expand Up @@ -60,21 +63,28 @@ export default function parse(md, prevLinks) {
// escaped
}
// Code/Indent blocks:
else if (t = (token[3] || token[4])) {
else if (!inline && (t = (token[3] || token[4]))) {
chunk = '<pre class="code '+(token[4]?'poetry':token[2].toLowerCase())+'"><code'+(token[2] ? ` class="language-${token[2].toLowerCase()}"` : '')+'>'+outdent(encodeAttr(t).replace(/^\n+|\n+$/g, ''))+'</code></pre>';
}
// > Quotes, -* lists:
else if (t = token[6]) {
if (t.match(/\./)) {
token[5] = token[5].replace(/^\d+/gm, '');
if (inline) {
// a list/quote marker inside a heading is literal text, not a block:
// keep the marker and parse only the rest inline (#115)
chunk = token[5].replace(/^(\s*(?:[>*+-]|\d+\.)\s+)([\s\S]*)$/, (s, marker, rest) => marker + parse(rest, links, inline));
}
inner = parse(outdent(token[5].replace(/^\s*[>*+.-]/gm, '')));
if (t=='>') t = 'blockquote';
else {
t = t.match(/\./) ? 'ol' : 'ul';
inner = inner.replace(/^(.*)(\n|$)/gm, '<li>$1</li>');
if (t.match(/\./)) {
token[5] = token[5].replace(/^\d+/gm, '');
}
inner = parse(outdent(token[5].replace(/^\s*[>*+.-]/gm, '')));
if (t=='>') t = 'blockquote';
else {
t = t.match(/\./) ? 'ol' : 'ul';
inner = inner.replace(/^(.*)(\n|$)/gm, '<li>$1</li>');
}
chunk = '<'+t+'>' + inner + '</'+t+'>';
}
chunk = '<'+t+'>' + inner + '</'+t+'>';
}
// Images:
else if (token[8]) {
Expand All @@ -89,9 +99,9 @@ export default function parse(md, prevLinks) {
chunk = '<a>';
}
// Headings:
else if (token[12] || token[14]) {
else if (!inline && (token[12] || token[14])) {
t = 'h' + (token[14] ? token[14].length : (token[13]>'=' ? 1 : 2));
chunk = '<'+t+'>' + parse(token[12] || token[15], links) + '</'+t+'>';
chunk = '<'+t+'>' + parse(token[12] || token[15], links, true) + '</'+t+'>';
}
// `code`:
else if (token[16]) {
Expand Down
11 changes: 11 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ describe('snarkdown()', () => {
expect(snarkdown('### I like tiny libraries')).to.equal('<h3>I like tiny libraries</h3>');
});

it('does not parse numbers in titles as lists', () => {
expect(snarkdown('### 1. Agreement')).to.equal('<h3>1. Agreement</h3>');
expect(snarkdown('### 2. Rights')).to.equal('<h3>2. Rights</h3>');
expect(snarkdown('### 1. **Agreement**')).to.equal('<h3>1. <strong>Agreement</strong></h3>');
});

it('does not parse list or quote markers in titles as blocks', () => {
expect(snarkdown('### - item')).to.equal('<h3>- item</h3>');
expect(snarkdown('### > quote')).to.equal('<h3>> quote</h3>');
});

it('parses titles with reference links', () => {
expect(
snarkdown('# I like [tiny libraries]\n\n[tiny libraries]: https://github.com/developit/snarkdown')
Expand Down