Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion assets/bundled/bbcode-parser.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion assets/bundled/bbcode-parser.min.js.map

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions assets/stylesheets/common/markdown-shims.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ pre:has(> code) + br {
aside.quote + br {
display: none;
}

:is(ul, ol, blockquote) + br:has(+ ul, + ol, + blockquote) {
display: none;
}
30 changes: 30 additions & 0 deletions bbcode-src/plugins/lineBreak.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const walk = (t, disableLineBreakConversion = false) => {
const tree = t;

if (Array.isArray(tree)) {
reduceWordsToLines(tree);
if (tree.some(isString)) {
// array contains strings. Might be md compatible
tree.unshift(MD_NEWLINE_INJECT);
Expand Down Expand Up @@ -69,6 +70,35 @@ const walk = (t, disableLineBreakConversion = false) => {
return tree;
};

/**
* Reduces the list into lines, so that we can process them by line.
* Performs in place.
* @param {(string|Object)[]} words
*/
const reduceWordsToLines = (words) => {
const lines = [];
let line = "";
for (const word of words) {
if (isString(word) && !isEOL(word)) {
line += word;
} else if (isString(word) && isEOL(word)) {
if (line) {
lines.push(line);
}
lines.push(word);
line = "";
} else {
lines.push(line);
lines.push(word);
line = "";
}
}
if (line) {
lines.push(line);
}
words.splice(0, words.length, ...lines);
};

/**
* Converts `\n` to `<br/>` self closing tag. Supply this as the last plugin in the preset lists
*
Expand Down
7 changes: 7 additions & 0 deletions bbcode-src/utils/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ const ESCAPABLES_REGEX =
/((\n|^)(?<fence>```+|~~~+)(?<fenceInfo>.*\n))|(?<bbcode>\[(?<bbcodeTag>i?code|plain)(=.*)?\])|(?<backtick>(?<tickStart>`{1,2})(.*)(?<tickEnd>\k<tickStart>))/im;
const MD_TABLE_REGEX = /^(\|[^\n]+\|\r?\n)((?:\| ?:?[-]+:? ?)+\|)(\n(?:\|[^\n]+\|\r?\n?)*)?$/m;

const MD_BROKEN_ORDERED_LIST = "</ol>\n<br><ol>";
const MD_BROKEN_UNORDERED_LIST = "</ul>\n<br><ul>";
const MD_BROKEN_BLOCKQUOTE = "</blockquote>\n<blockquote>";

/**
* Generates a random GUID.
*
Expand Down Expand Up @@ -156,4 +160,7 @@ export {
MD_TABLE_REGEX,
URL_REGEX_SINGLE_LINE,
ESCAPABLES_REGEX,
MD_BROKEN_ORDERED_LIST,
MD_BROKEN_UNORDERED_LIST,
MD_BROKEN_BLOCKQUOTE,
};
18 changes: 17 additions & 1 deletion bbcode-src/utils/postprocess.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { MD_NEWLINE_INJECT, MD_NEWLINE_INJECT_COMMENT, MD_NEWLINE_PRE_INJECT } from "./common";
import {
MD_BROKEN_BLOCKQUOTE,
MD_BROKEN_ORDERED_LIST,
MD_BROKEN_UNORDERED_LIST,
MD_NEWLINE_INJECT,
MD_NEWLINE_INJECT_COMMENT,
MD_NEWLINE_PRE_INJECT,
} from "./common";

/**
* Post Processing designed to fix issues with Markdown and BBCode that the parser can't fix.
Expand All @@ -17,6 +24,14 @@ function removeNewlineInjects(raw) {
return processed;
}

function cleanMultilineMDBlocks(raw) {
const processed = raw
.replaceAll(MD_BROKEN_ORDERED_LIST, "")
.replaceAll(MD_BROKEN_UNORDERED_LIST, "")
.replaceAll(MD_BROKEN_BLOCKQUOTE, "");
return processed;
}

/**
* Injects hoisted code blocks back into the raw string
* @param {string} raw input to inject hoisted code blocks into
Expand Down Expand Up @@ -81,6 +96,7 @@ export function postprocess(raw, data) {
removeNewlineInjects,
createClassStyleTagTemplate,
createScriptTagTemplate,
cleanMultilineMDBlocks,
renderHoistedCodeBlocks,
];
for (const postprocessor of postprocessors) {
Expand Down
Loading