/** * @import {State} from 'mdast-util-to-markdown' * @import {InlineCode, Parents} from 'mdast' */ inlineCode.peek = inlineCodePeek /** * Serialize an inline code node. * * @param {InlineCode} node * Node to serialize. * @param {Parents | undefined} _ * Parent node. * @param {State} state * Info passed around. * @returns {string} * Serialized markdown. */ export function inlineCode(node, _, state) { let value = node.value || '' let sequence = '`' let index = -1 // If there is a single grave accent on its own in the code, use a fence of // two. // If there are two in a row, use one. while (new RegExp('(^|[^`])' + sequence + '([^`]|$)').test(value)) { sequence += '`' } // If this is not just spaces or eols (tabs don’t count), and either the // first or last character are a space, eol, or tick, then pad with spaces. if ( /[^\n\r ]/.test(value) && ((/^[\n\r ]/.test(value) && /[\n\r ]$/.test(value)) || /^`|`$/.test(value)) ) { value = ' ' + value + ' ' } // We have a potential problem: certain characters after eols could result in // blocks being seen. // For example, if someone injected the string `'\n# b'`, then that would // result in an ATX heading. // We can’t escape characters in `inlineCode`, but because eols are // transformed to spaces when going from markdown to HTML anyway, we can swap // them out. while (++index < state.unsafe.length) { const pattern = state.unsafe[index] const expression = state.compilePattern(pattern) // Only look for `atBreak`s. // Btw: note that `atBreak` patterns will always start the regex at LF or // CR. if (!pattern.atBreak) continue /** @type {RegExpExecArray | null} */ let match while ((match = expression.exec(value))) { let position = match.index // Support CRLF (patterns only look for one of the characters). if ( value.charCodeAt(position) === 10 /* `\n` */ && value.charCodeAt(position - 1) === 13 /* `\r` */ ) { position-- } value = value.slice(0, position) + ' ' + value.slice(match.index + 1) } } return sequence + value + sequence } /** * Peek at this node. * * @returns {string} * Start of the serialized markdown. */ function inlineCodePeek() { return '`' }