Skip to content

Commit fa93ceb

Browse files
committed
Add removeEmptyLinePlugin to clean empty lines from HTML attributes
1 parent e361019 commit fa93ceb

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { isTagNode } from "@bbob/plugin-helper";
2+
const CONSECUTIVE_NEWLINE_REGEX = /\n{2,}/gm;
3+
4+
/**
5+
* Removes empty lines from a string
6+
* @param {string} text
7+
*/
8+
const removeEmptyLines = (text) => {
9+
return text.replace(CONSECUTIVE_NEWLINE_REGEX, "\n");
10+
};
11+
12+
/**
13+
* Removes empty lines from attributes
14+
* @type {import('@bbob/types').BBobPluginFunction}
15+
*/
16+
export const removeEmptyLinePlugin = (tree) => {
17+
return tree.walk((node) => {
18+
if (isTagNode(node) && node.attrs) {
19+
Object.keys(node.attrs).forEach((key) => {
20+
if (typeof node.attrs[key] === "string") {
21+
node.attrs[key] = removeEmptyLines(node.attrs[key]);
22+
}
23+
});
24+
}
25+
return node;
26+
});
27+
};

0 commit comments

Comments
 (0)