@phtml/markdown
Version:
Write markdown in HTML
1 lines • 5.26 kB
Source Map (JSON)
{"version":3,"file":"index.mjs","sources":["src/index.js"],"sourcesContent":["import marked from 'marked';\nimport phtml from 'phtml';\n\nexport default new phtml.Plugin('phtml-markdown', opts => {\n\tconst markdownAttributes = 'attr' in Object(opts)\n\t\t? [].concat(opts.attr || [])\n\t: ['md', 'markdown'];\n\tconst removeMarkdownAttributes = ('removeAttr' in Object(opts)\n\t\t? opts.removeAttr === true\n\t\t\t? markdownAttributes\n\t\t: opts.removeAttr === false\n\t\t\t? []\n\t\t: [].concat(opts.removeAttr || [])\n\t: markdownAttributes).filter(removeMarkdownAttribute => markdownAttributes.includes(removeMarkdownAttribute));\n\tconst markedOpts = Object(opts).marked;\n\n\tmarked.setOptions(markedOpts);\n\n\treturn {\n\t\tElement (node, result) {\n\t\t\tconst hasMarkdownAttribute = markdownAttributes.some(markdownAttribute => node.attrs.contains(markdownAttribute));\n\n\t\t\tif (hasMarkdownAttribute) {\n\t\t\t\tconst { constructor: Result } = result;\n\t\t\t\tconst innerHTML = node.innerHTML;\n\n\t\t\t\t// detect the initial indentation\n\t\t\t\tconst indentation = innerHTML.match(/^\\n*(\\s*)/)[1];\n\t\t\t\tconst indentationRegExp = new RegExp(`\\n${indentation}`, 'g');\n\n\t\t\t\t// strip the indentation\n\t\t\t\tconst unindentedHTML = innerHTML.replace(indentationRegExp, '\\n').trim();\n\n\t\t\t\t// parse the document as marked html\n\t\t\t\tconst markedHTML = marked(unindentedHTML).trim();\n\n\t\t\t\t// reprocess the marked html as nodes\n\t\t\t\tconst markedRoot = new Result(markedHTML, { from: node.source.from, visitors: result.visitors }).root;\n\n\t\t\t\t// conditionally strip the wrapping block when in a strict blocking element\n\t\t\t\tconst shouldStripBlock = stripBlockFromRegExp.test(node.name);\n\t\t\t\tconst hasMultipleNodes = markedRoot.nodes.length > 1;\n\n\t\t\t\tconst replacementContainerNodes = (shouldStripBlock && !hasMultipleNodes\n\t\t\t\t\t? markedRoot.first\n\t\t\t\t: markedRoot).nodes.slice(0);\n\n\t\t\t\t// remove the markdown attribute\n\t\t\t\tremoveMarkdownAttributes.forEach(markdownAttribute => {\n\t\t\t\t\tnode.attrs.remove(markdownAttribute);\n\t\t\t\t});\n\n\t\t\t\t// append the marked nodes\n\t\t\t\tnode.replaceAll(...replacementContainerNodes);\n\n\t\t\t\treturn replacementContainerNodes.reduce(\n\t\t\t\t\t(childPromise, childNode) => childPromise.then(\n\t\t\t\t\t\t() => childNode.visit(result)\n\t\t\t\t\t),\n\t\t\t\t\tPromise.resolve()\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t};\n});\n\nconst stripBlockFromRegExp = /^(abbr|acronym|b|bdo|big|button|cite|dfn|em|h1|h2|h3|h4|h5|h6|i|input|kbd|p|q|samp|select|small|span|strong|sub|sup|textarea|time|var)$/i;\n"],"names":["phtml","Plugin","opts","markdownAttributes","Object","concat","attr","removeMarkdownAttributes","removeAttr","filter","removeMarkdownAttribute","includes","markedOpts","marked","setOptions","Element","node","result","hasMarkdownAttribute","some","markdownAttribute","attrs","contains","constructor","Result","innerHTML","indentation","match","indentationRegExp","RegExp","unindentedHTML","replace","trim","markedHTML","markedRoot","from","source","visitors","root","shouldStripBlock","stripBlockFromRegExp","test","name","hasMultipleNodes","nodes","length","replacementContainerNodes","first","slice","forEach","remove","replaceAll","reduce","childPromise","childNode","then","visit","Promise","resolve"],"mappings":";;;AAGA,YAAe,IAAIA,KAAK,CAACC,MAAV,CAAiB,gBAAjB,EAAmCC,IAAI,IAAI;QACnDC,kBAAkB,GAAG,UAAUC,MAAM,CAACF,IAAD,CAAhB,GACxB,GAAGG,MAAH,CAAUH,IAAI,CAACI,IAAL,IAAa,EAAvB,CADwB,GAEzB,CAAC,IAAD,EAAO,UAAP,CAFF;QAGMC,wBAAwB,GAAG,CAAC,gBAAgBH,MAAM,CAACF,IAAD,CAAtB,GAC/BA,IAAI,CAACM,UAAL,KAAoB,IAApB,GACCL,kBADD,GAEAD,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