+/* eslint-disable no-param-reassign */
/* eslint-disable no-use-before-define */
import SimpleMarkdown from '@khanacademy/simple-markdown';
import { idRegex, parseIdUri } from './common';
plain: (node, output, state) => `${output(node.content, state)}\n\n`,
html: (node, output, state) => htmlTag('p', output(node.content, state)),
},
+ escape: {
+ ...defaultRules.escape,
+ plain: (node, output, state) => `\\${output(node.content, state)}`,
+ },
br: {
...defaultRules.br,
match: anyScopeRegex(/^ *\n/),
...plainRules,
heading: {
...defaultRules.heading,
- match: blockRegex(/^ *(#{1,6})([^\n:]*?(?: [^\n]*?)?)#* *(?:\n *)+\n/),
+ match: blockRegex(/^ *(#{1,6})([^\n:]*?(?: [^\n]*?)?)#* *(?:\n *)*\n/),
plain: (node, output, state) => {
const out = output(node.content, state);
if (state.kind === 'edit' || state.kind === 'notification' || node.level > 2) {
},
codeBlock: {
...defaultRules.codeBlock,
- plain: (node) => `\`\`\`${node.lang || ''}\n${node.content}\n\`\`\``,
+ plain: (node) => `\`\`\`${node.lang || ''}\n${node.content}\n\`\`\`\n`,
html: (node) => htmlTag('pre', htmlTag('code', sanitizeText(node.content), {
class: node.lang ? `language-${node.lang}` : undefined,
})),
},
list: {
...defaultRules.list,
- plain: (node, output, state) => `${node.items.map((item, i) => {
- const prefix = node.ordered ? `${node.start + i}. ` : '* ';
- return prefix + output(item, state).replace(/\n/g, `\n${' '.repeat(prefix.length)}`);
- }).join('\n')}\n`,
+ plain: (node, output, state) => {
+ const oldList = state._list;
+ state._list = true;
+
+ let items = node.items.map((item, i) => {
+ const prefix = node.ordered ? `${node.start + i}. ` : '* ';
+ return prefix + output(item, state).replace(/\n/g, `\n${' '.repeat(prefix.length)}`);
+ }).join('\n');
+
+ state._list = oldList;
+
+ if (!state._list) {
+ items += '\n\n';
+ }
+ return items;
+ },
},
def: undefined,
table: {
match: inlineRegex(/^¯\\_\(ツ\)_\/¯/),
parse: (capture) => ({ type: 'text', content: capture[0] }),
},
- escape: {
- ...defaultRules.escape,
- plain: (node, output, state) => `\\${output(node.content, state)}`,
- },
tableSeparator: {
...defaultRules.tableSeparator,
plain: () => ' | ',
},
inlineCode: {
...defaultRules.inlineCode,
+ match: inlineRegex(/^(`+)([^\n]*?[^`\n])\1(?!`)/),
plain: (node) => `\`${node.content}\``,
},
spoiler: {
case 'BLOCKQUOTE':
return [{ type: 'blockQuote', content: mapChildren(el) }];
case 'UL':
- return [{ type: 'list', items: mapChildren(el) }];
+ return [{ type: 'list', items: Array.from(el.childNodes).map(mapNode) }];
case 'OL':
return [{
type: 'list',
ordered: true,
start: Number(el.getAttribute('start')),
- items: mapChildren(el),
+ items: Array.from(el.childNodes).map(mapNode),
}];
case 'TABLE': {
const headerEl = Array.from(el.querySelector('thead > tr').childNodes);