diff --git a/src/index.js b/src/index.js index 3938c5c..a851ce5 100644 --- a/src/index.js +++ b/src/index.js @@ -22,8 +22,11 @@ function encodeAttr(str) { return (str+'').replace(/"/g, '"').replace(//g, '>'); } -/** 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 = '', @@ -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 = '
'+outdent(encodeAttr(t).replace(/^\n+|\n+$/g, ''))+'';
}
// > 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, '