Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | 21x 21x 21x 21x 21x 21x 21x 7x 7x 5x 2x 2x 2x 2x 2x 21x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 1x 3x 2x 2x 2x 2x 7x 7x 7x 7x 30x 30x 15x 15x 30x 7x 7x 2x 3x 1x 3x 3x 6x 6x 6x 6x 6x 6x 3x 3x 2x 3x 3x 3x 1x 1x 1x 2x 21x 6x 6x 6x 6x 2x 2x 4x 4x 21x 21x | 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(
'<div class="table-responsive"><table class="markbind-table table table-bordered table-striped">');
markdownIt.renderer.rules.table_close = _.constant('</table></div>');
// Bootstrap 5.3 removed the automatic darker bottom border on <thead>.
// Adding table-group-divider to <tbody> restores that visual separation.
markdownIt.renderer.rules.tbody_open = _.constant('<tbody class="table-group-divider">');
function getAttribute(token: Token, attr: string, deleteAttribute: boolean = false) {
const index = token.attrIndex(attr);
if (index === -1) {
return undefined;
}
Iif (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 => `<span class="${tok}">`).join('');
const re = /<span class="(.*?)">|<\/span>/g; // match all (<span class="xyz"> and </span>)
let matchArr = re.exec(line);
while (matchArr !== null) {
const [match, captureGrp] = matchArr;
if (match === '</span>') {
// pop from stack
tokenStack.shift();
} else {
// push to stack
tokenStack.unshift(captureGrp);
}
matchArr = re.exec(line);
}
const append = '</span>'.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 }) => {
Iif (highlightType === HIGHLIGHT_TYPES.WholeLine) {
return true;
}
Iif (highlightType === HIGHLIGHT_TYPES.WholeText) {
return true;
}
return false;
});
// Apply the WholeLine and WholeText highlight here
Iif (immediateHighlight) {
const highlightColor = immediateHighlight.color || '';
Iif (immediateHighlight.highlightType === HIGHLIGHT_TYPES.WholeLine) {
return Highlighter.highlightWholeLine(line, highlightColor);
}
Iif (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 || '' })),
),
);
Iif (boundsWithColors.length > 0) {
return Highlighter.highlightPartOfText(line, boundsWithColors);
}
// Default case: wrap in span
return `<span>${line}\n</span>`;
}).join('');
token.attrJoin('class', 'hljs');
if (highlighted) {
token.attrJoin('class', lang);
}
const heading = token.attrGet('heading');
const codeBlockContent = `<pre><code ${slf.renderAttrs(token)}>${str}</code></pre>`;
if (heading) {
const renderedHeading = markdownIt.renderInline(heading);
const headingStyle = (renderedHeading === heading)
? 'code-block-heading'
: 'code-block-heading inline-markdown-heading';
return '<div class="code-block">'
+ `<div class="${headingStyle}"><span>${renderedHeading}</span></div>`
+ `<div class="code-block-content">${codeBlockContent}</div>`
+ '</div>';
}
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 `<code${slf.renderAttrs(token)}>${
hljs.highlight(token.content, { language: lang, ignoreIllegals: true }).value
}</code>`;
}
token.attrSet('class', `${inlineClass} no-lang`);
return `<code${slf.renderAttrs(token)}>${
markdownIt.utils.escapeHtml(token.content)
}</code>`;
};
markdownIt.use(markdownItEmoji, {
defs: fixedNumberEmojiDefs,
});
(markdownIt as any).createDoubleDelimiterInlineRule = createDoubleDelimiterInlineRule;
export { markdownIt };
|