Skip to content

Commit 515269b

Browse files
authored
Add removeEmptyLinePlugin to clean empty lines from HTML attributes (#144)
1 parent a3b3e72 commit 515269b

4 files changed

Lines changed: 32 additions & 3 deletions

File tree

assets/bundled/bbcode-parser.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/bundled/bbcode-parser.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bbcode-src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import bbob from "@bbob/core";
22
import { render } from "@bbob/html";
33
import { lineBreakPlugin } from "./plugins/lineBreak";
44
import { preserveWhitespace } from "./plugins/preserveWhitespace";
5+
import { removeEmptyLinePlugin } from "./plugins/removeEmptyLinesInAttr";
56
import { availableTags, preset, preventParsing } from "./preset";
67
import { postprocess } from "./utils/postprocess";
78
import { preprocessRaw } from "./utils/preprocess";
@@ -25,7 +26,7 @@ export const RpNBBCode = (code, opts) => {
2526
if (opts.preserveWhitespace) {
2627
plugins.push(preserveWhitespace());
2728
}
28-
plugins.push(lineBreakPlugin());
29+
plugins.push(lineBreakPlugin(), removeEmptyLinePlugin);
2930
const [preprocessed, preprocessedData] = preprocessRaw(code);
3031
return bbob(plugins).process(preprocessed, {
3132
render,
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { isTagNode } from "@bbob/plugin-helper";
2+
3+
const CONSECUTIVE_NEWLINE_REGEX = /\n{2,}/gm;
4+
5+
/**
6+
* Removes empty lines from a string
7+
* @param {string} text
8+
*/
9+
const removeEmptyLines = (text) => {
10+
return text.replace(CONSECUTIVE_NEWLINE_REGEX, "\n");
11+
};
12+
13+
/**
14+
* Removes empty lines from attributes
15+
* @type {import('@bbob/types').BBobPluginFunction}
16+
*/
17+
export const removeEmptyLinePlugin = (tree) => {
18+
return tree.walk((node) => {
19+
if (isTagNode(node) && node.attrs) {
20+
Object.keys(node.attrs).forEach((key) => {
21+
if (typeof node.attrs[key] === "string") {
22+
node.attrs[key] = removeEmptyLines(node.attrs[key]);
23+
}
24+
});
25+
}
26+
return node;
27+
});
28+
};

0 commit comments

Comments
 (0)