import katex from 'katex'; import hljs from 'highlight.js'; import markdownItImport, { Options } from 'markdown-it'; import _ from 'lodash'; import Renderer from 'markdown-it/lib/renderer'; import Token from 'markdown-it/lib/token'; import markdownItMark from 'markdown-it-mark'; import markdownItSub from 'markdown-it-sub'; import markdownItSup from 'markdown-it-sup'; import markdownItImsize from '@tlylt/markdown-it-imsize'; import markdownItTableOfContents from 'markdown-it-table-of-contents'; import markdownItTaskLists from 'markdown-it-task-lists'; import markdownItLinkifyImages from 'markdown-it-linkify-images'; import markdownItTexmath from 'markdown-it-texmath'; import markdownItAttrs from 'markdown-it-attrs'; import markdownItEmoji from 'markdown-it-emoji'; import { Highlighter } from './highlight/Highlighter.js'; import { HighlightRule, HIGHLIGHT_TYPES } from './highlight/HighlightRule.js'; import * as logger from '../../utils/logger.js'; import { altFrontmatterPlugin } from './plugins/markdown-it-alt-frontmatter.js'; import { markdownItIconsPlugin } from './plugins/markdown-it-icons.js'; import { centertext_plugin } from './plugins/markdown-it-center-text.js'; import { colourTextPlugin } from './plugins/markdown-it-colour-text.js'; import { createDoubleDelimiterInlineRule } from './plugins/markdown-it-double-delimiter.js'; import { footnotePlugin } from './plugins/markdown-it-footnotes.js'; import { radioButtonPlugin } from './plugins/markdown-it-radio-button.js'; import { setup as blockEmbedPlugin } from './plugins/markdown-it-block-embed/index.js'; import { emojiData as fixedNumberEmojiDefs } from './patches/markdown-it-emoji-fixed.js'; const markdownIt = markdownItImport({ html: true, linkify: true }); markdownIt.linkify.set({ fuzzyLink: false }); // markdown-it plugins markdownIt.use(createDoubleDelimiterInlineRule('%%', 'dimmed', 'emphasis')) .use(createDoubleDelimiterInlineRule('!!', 'underline', 'dimmed')) .use(createDoubleDelimiterInlineRule('++', 'large', 'underline')) .use(createDoubleDelimiterInlineRule('--', 'small', 'large')); markdownIt.use(markdownItMark) .use(markdownItSub) .use(markdownItSup) .use(markdownItImsize, { autofill: false }) .use(markdownItTableOfContents) .use(markdownItTaskLists, { enabled: true }) .use(markdownItLinkifyImages, { imgClass: 'img-fluid' }) .use(markdownItTexmath, { engine: katex, delimiters: ['dollars', 'brackets'] }) .use(markdownItAttrs) .use(radioButtonPlugin, { enabled: true, label: true }) .use(blockEmbedPlugin) .use(markdownItIconsPlugin) .use(footnotePlugin) .use(centertext_plugin) .use(colourTextPlugin) .use(altFrontmatterPlugin); // fix table style markdownIt.renderer.rules.table_open = _.constant( '
'); markdownIt.renderer.rules.table_close = _.constant('
'); // Bootstrap 5.3 removed the automatic darker bottom border on . // Adding table-group-divider to restores that visual separation. markdownIt.renderer.rules.tbody_open = _.constant(''); function getAttribute(token: Token, attr: string, deleteAttribute: boolean = false) { const index = token.attrIndex(attr); if (index === -1) { return undefined; } if (token.attrs === null) { return undefined; } // tokens are stored as an array of two-element-arrays: // e.g. [ ['highlight-lines', '1,2,3'], ['start-from', '1'] ] const value = token.attrs[index][1]; if (deleteAttribute) { token.attrs.splice(index, 1); } return value; } // syntax highlight code fences and add line numbers markdownIt.renderer.rules.fence = (tokens: Token[], idx: number, options: Options, env: any, slf: Renderer) => { const token = tokens[idx]; const lang = token.info || ''; let str = token.content; str = str.replace(/\t/g, ' '); // Convert tabs to 4 spaces by default let highlighted = false; let lines: string[] = []; const startFromRawValue = getAttribute(token, 'start-from', true); const startFromOneBased = startFromRawValue === undefined ? 1 : Math.max(1, parseInt(startFromRawValue, 10) || 1); const startFromZeroBased = startFromOneBased - 1; if (startFromRawValue) { if (startFromOneBased > 1) { // counter is incremented on each span, so we need to subtract 1 token.attrJoin('style', `counter-reset: line ${startFromZeroBased};`); } const existingClass = getAttribute(token, 'class') || ''; const lineNumbersRegex = /(^|\s)line-numbers($|\s)/; const hasLineNumbers = lineNumbersRegex.test(existingClass); if (!hasLineNumbers) { token.attrJoin('class', 'line-numbers'); } } const highlightLinesInput = getAttribute(token, 'highlight-lines', true); let highlightRules: HighlightRule[] = []; if (highlightLinesInput) { highlightRules = HighlightRule.parseAllRules(highlightLinesInput, -startFromZeroBased, str); } if (lang && hljs.getLanguage(lang)) { try { /* With highlightjs version >= v10.7.0, usage of continuation is deprecated For the purposes of line-by-line highlighting, we have to first highlight the whole block, then split the resulting html string according to '\n', and add the corresponding opening and closing tags for the html string to be well-formed and maintain the correct state per line. Ref: https://github.com/MarkBind/markbind/pull/1521 */ lines = hljs.highlight(str, { language: lang, ignoreIllegals: true }).value.split('\n'); const tokenStack: string[] = []; lines = lines.map((line) => { const prepend = tokenStack.map(tok => ``).join(''); const re = /|<\/span>/g; // match all ( and ) let matchArr = re.exec(line); while (matchArr !== null) { const [match, captureGrp] = matchArr; if (match === '') { // pop from stack tokenStack.shift(); } else { // push to stack tokenStack.unshift(captureGrp); } matchArr = re.exec(line); } const append = ''.repeat(tokenStack.length); return prepend + line + append; }); highlighted = true; } catch (ex) { logger.error(`Error processing code block line ${ex}`); } } if (!highlighted) { lines = markdownIt.utils.escapeHtml(str).split('\n'); } lines.pop(); // last line is always a single '\n' newline, so we remove it str = lines.map((line, index) => { const currentLineNumber = index + 1; // Firstly, we determine if there are whole line/text highlights const immediateHighlight = highlightRules.flatMap(rule => rule.getHighlightType(currentLineNumber), ).find(({ highlightType }) => { if (highlightType === HIGHLIGHT_TYPES.WholeLine) { return true; } if (highlightType === HIGHLIGHT_TYPES.WholeText) { return true; } return false; }); // Apply the WholeLine and WholeText highlight here if (immediateHighlight) { const highlightColor = immediateHighlight.color || ''; if (immediateHighlight.highlightType === HIGHLIGHT_TYPES.WholeLine) { return Highlighter.highlightWholeLine(line, highlightColor); } if (immediateHighlight.highlightType === HIGHLIGHT_TYPES.WholeText) { return Highlighter.highlightWholeText(line, highlightColor); } } // Next, handle partial text highlight logic here const boundsWithColors = highlightRules.flatMap(rule => rule.getHighlightType(currentLineNumber) .filter(({ highlightType, bounds }) => highlightType === HIGHLIGHT_TYPES.PartialText && bounds, ) .flatMap(({ bounds, color }) => bounds!.map(bound => ({ bounds: bound, color: color || '' })), ), ); if (boundsWithColors.length > 0) { return Highlighter.highlightPartOfText(line, boundsWithColors); } // Default case: wrap in span return `${line}\n`; }).join(''); token.attrJoin('class', 'hljs'); if (highlighted) { token.attrJoin('class', lang); } const heading = token.attrGet('heading'); const codeBlockContent = `
${str}
`; if (heading) { const renderedHeading = markdownIt.renderInline(heading); const headingStyle = (renderedHeading === heading) ? 'code-block-heading' : 'code-block-heading inline-markdown-heading'; return '
' + `
${renderedHeading}
` + `
${codeBlockContent}
` + '
'; } return codeBlockContent; }; // highlight inline code markdownIt.renderer.rules.code_inline = (tokens: Token[], idx: number, options: Options, env: any, slf: Renderer) => { const token = tokens[idx]; const lang = token.attrGet('class'); const inlineClass = 'hljs inline'; if (lang && hljs.getLanguage(lang)) { token.attrSet('class', `${inlineClass} ${lang}`); return `${ hljs.highlight(token.content, { language: lang, ignoreIllegals: true }).value }`; } token.attrSet('class', `${inlineClass} no-lang`); return `${ markdownIt.utils.escapeHtml(token.content) }`; }; markdownIt.use(markdownItEmoji, { defs: fixedNumberEmojiDefs, }); (markdownIt as any).createDoubleDelimiterInlineRule = createDoubleDelimiterInlineRule; export { markdownIt };