UNPKG

micro-mdx-parser

Version:

A tiny parser to convert markdown or html into JSON

101 lines (87 loc) 3.2 kB
// https://regex101.com/r/oPKKoC/1 const REMOVE_CODE_BLOCK_RE = /^(?:[A-Za-z \t]*)?(```(?:[A-Za-z]*)?\n(?:[\s\S]*?)```)([A-Za-z \t]*)*$/gm // remove inline `code` blocks const REMOVE_INLINE_CODE_BLOCK = /`[^`\n]*`/g // https://regex101.com/r/ydaz8U/8 const CODE_BLOCK_RE = /^(>(?: >)*)?([A-Za-z \t]*)([`~]{3,4})([A-Za-z]*)?(.*)?\n\s*([`~]{3,4})?\s*([\s\S]*?)(\6)?[\s]*?(\3)([A-Za-z \t]*)*$/gm // https://regex101.com/r/ydaz8U/14 const CODE_BLOCK_HTML_RE = /^(>(?: >)*)?([A-Za-z \t]*)([`~]{3,4})([A-Za-z]*)?(.*)?\n\s*([`~]{3,4})?\s*([\s\S]*?)(\6)?[\s]*?(\3)([A-Za-z \t]*)*$|[^`](?:<(pre)\b([^>]*)>*(?:>\s*(<code\b([^>]*)>)?([\s\S]*?)(<\/code>\s*)?<\/pre>))/gm // https://regex101.com/r/ZEMXNE/3 const MATCH_JS_COMMENT = /^(\/\*[\s\S]*?\*\/|^(\/\/ +[^\n]+(?:\n\/\/*[^\n]+)*)+)\n?/ function findCodeBlocks(block) { let matches let blocks = [] while ((matches = CODE_BLOCK_HTML_RE.exec(block)) !== null) { if (matches.index === CODE_BLOCK_HTML_RE.lastIndex) { CODE_BLOCK_HTML_RE.lastIndex++ // avoid infinite loops with zero-width matches } const [ match, insideBlockQuote, // 1 prefix = '', // 2 openTicks, // 3 syntax, // 4 props, // 5 innerTicksOpen = '', // 6 _content, // 7 innerTicksClose = '', // 8 closeTicks, // 9 postFix, // 10 preTag, // 11 preTagAttrs, // 12 codeTag, // 13 codeTagAttrs, // 14 preCodeContent, // 15 ] = matches //* // debug // console.log('match') // console.log(match) // console.log('preCodeContent', preCodeContent) // console.log('insideBlockQuote', insideBlockQuote) // console.log('prefix', prefix) // console.log('openTicks', openTicks) // console.log('syntax', syntax) // console.log('props', props) // console.log('innerTicksOpen', innerTicksOpen) // console.log('_content>>', _content) // console.log('innerTicksClose', innerTicksClose) // console.log('closeTicks', closeTicks) // console.log('postFix', postFix) // console.log('insideBlockQuote', insideBlockQuote) // console.log('prefix', `"${prefix}"`) /** */ // let codeContent = (preCodeContent) ? preCodeContent.trim() : _content // if (insideBlockQuote) { // // replace leading block quote > > arrows // codeContent = _content.replace(new RegExp(`^${insideBlockQuote}`, 'gm'), '') // if (prefix) { // // trim leading spaces from codeContent if prefix exists // codeContent = codeContent.replace(new RegExp(`^${prefix}`, ''), '') // } // } // let finalCode = prefix + innerTicksOpen + codeContent + innerTicksClose // const codeBlock = {} // let hasError = false // if (includePositions) { // codeBlock.index = matches.index // } // if (props) { // codeBlock.propsRaw = props // codeBlock.props = parse(props) // } blocks.push(match) } return blocks } function removeCode(text = '') { return text .replace(CODE_BLOCK_HTML_RE, '') .replace(REMOVE_INLINE_CODE_BLOCK, '') } module.exports = { findCodeBlocks, removeCode, CODE_BLOCK_RE, CODE_BLOCK_HTML_RE // REMOVE_CODE_BLOCK_RE }