Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: support [code] in blockquotes #26182

Merged
merged 1 commit into from Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,9 +1,8 @@
let isWhiteSpace;

function trailingSpaceOnly(src, start, max) {
let i;
for (i = start; i < max; i++) {
let code = src.charCodeAt(i);
for (let i = start; i < max; i++) {
const code = src.charCodeAt(i);
if (code === 0x0a) {
return true;
}
Expand Down Expand Up @@ -35,10 +34,7 @@ export function parseBBCodeTag(src, start, max, multiline) {
}

for (i = start + 1; i < max; i++) {
let letter = src[i];
if (
!((letter >= "a" && letter <= "z") || (letter >= "A" && letter <= "Z"))
) {
if (!/[a-z]/i.test(src[i])) {
break;
}
}
Expand All @@ -63,9 +59,7 @@ export function parseBBCodeTag(src, start, max, multiline) {
}

for (; i < max; i++) {
let letter = src[i];

if (letter === "]") {
if (src[i] === "]") {
closed = true;
break;
}
Expand All @@ -90,9 +84,7 @@ export function parseBBCodeTag(src, start, max, multiline) {
val = match[1] || match[5];

if (val) {
val = val.trim();
val = val.replace(/^["'“”](.*)["'“”]$/, "$1");
attrs[key] = val;
attrs[key] = val.trim().replace(/^["'“”](.*)["'“”]$/, "$1");
}
}
}
Expand Down Expand Up @@ -227,7 +219,6 @@ function applyBBCode(state, startLine, endLine, silent, md) {
nextLine = startLine;

// We might have a single inline bbcode

let closeTag = findInlineCloseTag(state, info, start + info.length, max);

if (!closeTag) {
Expand Down Expand Up @@ -256,10 +247,7 @@ function applyBBCode(state, startLine, endLine, silent, md) {
if (startLine === nextLine) {
content = state.src.slice(start + info.length, closeTag.start);
} else {
content = state.src.slice(
state.bMarks[startLine + 1],
state.eMarks[nextLine - 1]
);
content = state.getLines(startLine + 1, nextLine, 0, false);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is the actual bug fix. Doing a start.src.slice was not taking into account being nested inside a block quote for example (which prepends a > on every line).

The getLines function does all the heavy lifting for us 👏

}

if (!rule.replace.call(this, state, info, content)) {
Expand Down Expand Up @@ -343,30 +331,27 @@ function applyBBCode(state, startLine, endLine, silent, md) {

export function setup(helper) {
helper.registerPlugin((md) => {
const ruler = md.block.bbcode.ruler;
isWhiteSpace = md.utils.isWhiteSpace;

ruler.push("excerpt", {
md.block.bbcode.ruler.push("excerpt", {
tag: "excerpt",
wrap: "div.excerpt",
});

ruler.push("code", {
md.block.bbcode.ruler.push("code", {
tag: "code",
replace(state, tagInfo, content) {
let token;
token = state.push("fence", "code", 0);
let token = state.push("fence", "code", 0);
token.content = content;
return true;
},
});

isWhiteSpace = md.utils.isWhiteSpace;
md.block.ruler.after(
"fence",
"bbcode",
(state, startLine, endLine, silent) => {
return applyBBCode(state, startLine, endLine, silent, md);
},
(state, startLine, endLine, silent) =>
applyBBCode(state, startLine, endLine, silent, md),
{ alt: ["paragraph", "reference", "blockquote", "list"] }
);
});
Expand Down
Expand Up @@ -1212,6 +1212,11 @@ eviltrout</p>
'<pre><code class="lang-auto"> s</code></pre>',
"it doesn't trim leading whitespace"
);
assert.cooked(
"> [code]\n> line 1\n> line 2\n> line 3\n> [/code]",
'<blockquote>\n<pre><code class="lang-auto">line 1\nline 2\nline 3</code></pre>\n</blockquote>',
"supports quoting a whole [code] block"
);
});

test("tags with arguments", function (assert) {
Expand Down