mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
DEV: Upgrade Markdown.it to v13.0.1 (#17099)
Updates markdown-it to v13.0.1 Noteworthy changes: * `markdownit()` is now available on `globalThis` instead of `window`. * The `text_collapse` rule was renamed to `fragments_join` which affected the `bbcode-inline` implementation. * The `linkify` rule was added to the `inline` chain which affected the handling of the `[url]` BBCode. If available, our implementation reuses `link_open` and `link_close` tokens created by linkify in order to prevent duplicate links. * The rendered HTML for code changed slightly. There's now a linebreak before the `</code>` tag. The tests were adjusted accordingly.
This commit is contained in:
@@ -877,7 +877,7 @@ eviltrout</p>
|
||||
|
||||
assert.cooked(
|
||||
" ```\n hello\n ```",
|
||||
"<pre><code>```\nhello\n```</code></pre>",
|
||||
"<pre><code>```\nhello\n```\n</code></pre>",
|
||||
"only detect ``` at the beginning of lines"
|
||||
);
|
||||
|
||||
@@ -925,13 +925,13 @@ eviltrout</p>
|
||||
|
||||
assert.cooked(
|
||||
" <pre>test</pre>",
|
||||
"<pre><code><pre>test</pre></code></pre>",
|
||||
"<pre><code><pre>test</pre>\n</code></pre>",
|
||||
"it does not parse other block types in markdown code blocks"
|
||||
);
|
||||
|
||||
assert.cooked(
|
||||
" [quote]test[/quote]",
|
||||
"<pre><code>[quote]test[/quote]</code></pre>",
|
||||
"<pre><code>[quote]test[/quote]\n</code></pre>",
|
||||
"it does not parse other block types in markdown code blocks"
|
||||
);
|
||||
|
||||
|
||||
@@ -344,9 +344,9 @@ function buildCustomMarkdownCookFunction(engineOpts, defaultEngineOpts) {
|
||||
function createMarkdownItEngineWithOpts(markdownitOpts, ruleOverrides) {
|
||||
if (ruleOverrides !== undefined) {
|
||||
// Preset for "zero", https://github.com/markdown-it/markdown-it/blob/master/lib/presets/zero.js
|
||||
return window.markdownit("zero", markdownitOpts).enable(ruleOverrides);
|
||||
return globalThis.markdownit("zero", markdownitOpts).enable(ruleOverrides);
|
||||
}
|
||||
return window.markdownit(markdownitOpts);
|
||||
return globalThis.markdownit(markdownitOpts);
|
||||
}
|
||||
|
||||
function overrideMarkdownFeatures(features, featureOverrides) {
|
||||
|
||||
@@ -85,7 +85,6 @@ function processBBCode(state, silent) {
|
||||
let i,
|
||||
startDelim,
|
||||
endDelim,
|
||||
token,
|
||||
tagInfo,
|
||||
delimiters = state.delimiters,
|
||||
max = delimiters.length;
|
||||
@@ -108,9 +107,11 @@ function processBBCode(state, silent) {
|
||||
|
||||
endDelim = delimiters[startDelim.end];
|
||||
|
||||
token = state.tokens[startDelim.token];
|
||||
let tag, className;
|
||||
|
||||
const startToken = state.tokens[startDelim.token];
|
||||
const endToken = state.tokens[endDelim.token];
|
||||
|
||||
if (typeof tagInfo.rule.wrap === "function") {
|
||||
let content = "";
|
||||
for (let j = startDelim.token + 1; j < endDelim.token; j++) {
|
||||
@@ -119,7 +120,7 @@ function processBBCode(state, silent) {
|
||||
content += inner.content;
|
||||
}
|
||||
}
|
||||
tagInfo.rule.wrap(token, state.tokens[endDelim.token], tagInfo, content);
|
||||
tagInfo.rule.wrap(startToken, endToken, tagInfo, content, state);
|
||||
continue;
|
||||
} else {
|
||||
let split = tagInfo.rule.wrap.split(".");
|
||||
@@ -127,21 +128,20 @@ function processBBCode(state, silent) {
|
||||
className = split.slice(1).join(" ");
|
||||
}
|
||||
|
||||
token.type = "bbcode_" + tagInfo.tag + "_open";
|
||||
token.tag = tag;
|
||||
startToken.type = "bbcode_" + tagInfo.tag + "_open";
|
||||
startToken.tag = tag;
|
||||
if (className) {
|
||||
token.attrs = [["class", className]];
|
||||
startToken.attrs = [["class", className]];
|
||||
}
|
||||
token.nesting = 1;
|
||||
token.markup = token.content;
|
||||
token.content = "";
|
||||
startToken.nesting = 1;
|
||||
startToken.markup = startToken.content;
|
||||
startToken.content = "";
|
||||
|
||||
token = state.tokens[endDelim.token];
|
||||
token.type = "bbcode_" + tagInfo.tag + "_close";
|
||||
token.tag = tag;
|
||||
token.nesting = -1;
|
||||
token.markup = token.content;
|
||||
token.content = "";
|
||||
endToken.type = "bbcode_" + tagInfo.tag + "_close";
|
||||
endToken.tag = tag;
|
||||
endToken.nesting = -1;
|
||||
endToken.markup = startToken.content;
|
||||
endToken.content = "";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -164,7 +164,7 @@ export function setup(helper) {
|
||||
md.inline.ruler.push("bbcode-inline", (state, silent) =>
|
||||
tokenizeBBCode(state, silent, ruler)
|
||||
);
|
||||
md.inline.ruler2.before("text_collapse", "bbcode-inline", processBBCode);
|
||||
md.inline.ruler2.before("fragments_join", "bbcode-inline", processBBCode);
|
||||
|
||||
ruler.push("code", {
|
||||
tag: "code",
|
||||
@@ -176,13 +176,35 @@ export function setup(helper) {
|
||||
},
|
||||
});
|
||||
|
||||
const simpleUrlRegex = /^http[s]?:\/\//;
|
||||
const simpleUrlRegex = /^https?:\/\//;
|
||||
ruler.push("url", {
|
||||
tag: "url",
|
||||
wrap(startToken, endToken, tagInfo, content) {
|
||||
wrap(startToken, endToken, tagInfo, content, state) {
|
||||
const url = (tagInfo.attrs["_default"] || content).trim();
|
||||
let linkifyFound = false;
|
||||
|
||||
if (simpleUrlRegex.test(url)) {
|
||||
if (state.md.options.linkify) {
|
||||
const tokens = state.tokens;
|
||||
const startIndex = tokens.indexOf(startToken);
|
||||
const endIndex = tokens.indexOf(endToken);
|
||||
|
||||
// reuse existing tokens from linkify if they exist
|
||||
for (let index = startIndex + 1; index < endIndex; index++) {
|
||||
const token = tokens[index];
|
||||
|
||||
if (
|
||||
token.markup === "linkify" &&
|
||||
token.info === "auto" &&
|
||||
token.type === "link_open"
|
||||
) {
|
||||
linkifyFound = true;
|
||||
token.attrs.push(["data-bbcode", "true"]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!linkifyFound && simpleUrlRegex.test(url)) {
|
||||
startToken.type = "link_open";
|
||||
startToken.tag = "a";
|
||||
startToken.attrs = [
|
||||
@@ -214,7 +236,7 @@ export function setup(helper) {
|
||||
tag: "email",
|
||||
replace(state, tagInfo, content) {
|
||||
let token;
|
||||
let email = tagInfo.attrs["_default"] || content;
|
||||
const email = tagInfo.attrs["_default"] || content;
|
||||
|
||||
token = state.push("link_open", "a", 1);
|
||||
token.attrs = [
|
||||
|
||||
Reference in New Issue
Block a user