Skip to content

Commit

Permalink
Fix preprocess node single attr via tag start pos workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
Alteras1 committed Aug 6, 2024
1 parent b79846e commit 81f39a7
Show file tree
Hide file tree
Showing 38 changed files with 124 additions and 124 deletions.
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.

1 change: 1 addition & 0 deletions bbcode-src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const RpNBBCode = (code, opts) => {
...options,
data: {
...preprocessedData,
raw: preprocessed,
previewing: opts.previewing,
fonts: new Set(),
styles: [],
Expand Down
10 changes: 5 additions & 5 deletions bbcode-src/tags/accordion.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { isStringNode, isTagNode, TagNode } from "@bbob/plugin-helper";
import {
generateGUID,
preprocessAttr,
regexIndexOf,
toNode,
toOriginalStartTag,
} from "../utils/common";
import { TagNode, isStringNode, isTagNode } from "@bbob/plugin-helper";

const SLIDE_TITLE_OPEN = Symbol("slide-title-open");
const SLIDE_TITLE_CLOSE = Symbol("slide-title-close");
Expand All @@ -19,7 +19,7 @@ const SLIDE_REGEX =
*
* [accordion][slide=name]content[/slide][/accordion]
*/
const accordion = (node) => {
const accordion = (node, options) => {
const groupId = generateGUID();

// add support for existing {slide} tags style, due to copious amounts of existing content
Expand All @@ -39,7 +39,7 @@ const accordion = (node) => {
return [toOriginalStartTag(node), ...node.content, node.toTagEnd()];
}

const attrs = preprocessAttr(node.attrs);
const attrs = preprocessAttr(node, options.data.raw);

if (attrs._default) {
/** @type {string[]} */
Expand Down Expand Up @@ -188,12 +188,12 @@ function markerToString(marker) {
}
}

const slide = (node) => {
const slide = (node, options) => {
if (!node.isValid) {
// not inside an [accordion] tag
return [toOriginalStartTag(node), ...node.content, node.toTagEnd()];
}
const attrs = preprocessAttr(node.attrs);
const attrs = preprocessAttr(node, options.data.raw);
let title = [attrs.title || attrs._default || "Slide"];
let isOpen = !!attrs.open || false;
let titleAlign = attrs.left ? "left" : attrs.right ? "right" : attrs.center ? "center" : "left";
Expand Down
8 changes: 4 additions & 4 deletions bbcode-src/tags/anchor.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import { preprocessAttr, toNode } from "../utils/common";
*/
export const anchor = {
// name is not valid in HTML5; however, it correctly displays back while id does not
a: (node) => {
const attrs = preprocessAttr(node.attrs)._default || "";
a: (node, options) => {
const attrs = preprocessAttr(node, options.data.raw)._default || "";
return toNode(
"a",
{ id: `user-anchor-${attrs.trim()}`, name: `user-anchor-${attrs.trim()}` },
node.content,
);
},
goto: (node) => {
const attrs = preprocessAttr(node.attrs)._default || "";
goto: (node, options) => {
const attrs = preprocessAttr(node, options.data.raw)._default || "";
return toNode("a", { href: `#user-anchor-${attrs.trim()}` }, node.content);
},
};
6 changes: 3 additions & 3 deletions bbcode-src/tags/animation.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { preprocessAttr, toOriginalStartTag } from "../utils/common";
import { isStringNode, isTagNode } from "@bbob/plugin-helper";
import { preprocessAttr, toOriginalStartTag } from "../utils/common";

/**
* Renders css Keyframes
Expand All @@ -15,13 +15,13 @@ export const animation = (node, options) => {
}
const commonId = options.data.previewing ? "preview" : options.data.commonGUID;

const name = preprocessAttr(node.attrs)?._default || "";
const name = preprocessAttr(node, options.data.raw)?._default || "";
const keyframes = node.content
.filter((n) => isTagNode(n) && n.tag === "keyframe")
.map((content) => {
content.isValid = true;
/** @type {string} */
const ident = preprocessAttr(content.attrs)._default || "";
const ident = preprocessAttr(content, options.data.raw)._default || "";
content.ident = ident + (ident.match(/^\d+$/) ? "%" : "");
const cleanContent = content.content
.filter(isStringNode)
Expand Down
4 changes: 2 additions & 2 deletions bbcode-src/tags/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { preprocessAttr, toNode } from "../utils/common";
* Add [bg] tag
* @example [bg=red]Hello[/bg]
*/
export const bg = (node) => {
const color = preprocessAttr(node.attrs)._default;
export const bg = (node, options) => {
const color = preprocessAttr(node, options.data.raw)._default;
return toNode(
"div",
{
Expand Down
4 changes: 2 additions & 2 deletions bbcode-src/tags/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { preprocessAttr, toNode } from "../utils/common";
* Add [block] tag
* @example [block=treasure]content[/block]
*/
export const block = (node) => {
export const block = (node, options) => {
const defaultOp = "block";
const blockAttr = (preprocessAttr(node.attrs)._default || defaultOp).toLowerCase();
const blockAttr = (preprocessAttr(node, options.data.raw)._default || defaultOp).toLowerCase();

const OPTIONS = [
"block",
Expand Down
4 changes: 2 additions & 2 deletions bbcode-src/tags/blockquote.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { preprocessAttr, toNode } from "../utils/common";
* @file Adds [blockquote] to bbcode
* @example [blockquote=author]content[/blockquote]
*/
export const blockquote = (node) => {
const author = preprocessAttr(node.attrs)._default || "";
export const blockquote = (node, options) => {
const author = preprocessAttr(node, options.data.raw)._default || "";

return toNode("div", { class: "bb-blockquote" }, [
toNode("div", { class: "bb-blockquote-left" }),
Expand Down
6 changes: 3 additions & 3 deletions bbcode-src/tags/border.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { preprocessAttr, toNode } from "../utils/common";

export const border = (node) => {
const val = preprocessAttr(node.attrs)._default;
export const border = (node, options) => {
const val = preprocessAttr(node, options.data.raw)._default;
return toNode(
"div",
{
style: `border: ${val};`,
class: "bb-border",
},
node.content
node.content,
);
};
4 changes: 2 additions & 2 deletions bbcode-src/tags/centerblock.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { preprocessAttr, toNode } from "../utils/common";

export const centerblock = (node) => {
const percentageInput = preprocessAttr(node.attrs)._default || "50";
export const centerblock = (node, options) => {
const percentageInput = preprocessAttr(node, options.data.raw)._default || "50";
return toNode("div", { style: `margin: 0 auto; width: ${percentageInput}%` }, node.content);
};
4 changes: 2 additions & 2 deletions bbcode-src/tags/check.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { preprocessAttr, toNode } from "../utils/common";

export const check = (node) => {
const attrs = preprocessAttr(node.attrs)._default || "dot";
export const check = (node, options) => {
const attrs = preprocessAttr(node, options.data.raw)._default || "dot";
return toNode("div", { class: `bb-check`, "data-type": attrs }, node.content);
};
2 changes: 1 addition & 1 deletion bbcode-src/tags/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { preprocessAttr } from "../utils/common";
* [class name="className" selector=""]content[/class]
*/
export const classStyle = (node, options) => {
const attrs = preprocessAttr(node.attrs);
const attrs = preprocessAttr(node);
const nameAttr = attrs.name || attrs._default;

if (!options.data.previewing && !options.data.commonGUID) {
Expand Down
2 changes: 1 addition & 1 deletion bbcode-src/tags/code.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { preprocessAttr } from "../utils/common";
* processes [code] tag and returns a fenced code block
*/
export const code = (node) => {
const lang = preprocessAttr(node.attrs)._default || "bbcode";
const lang = preprocessAttr(node)._default || "bbcode";
return {
isWhitespaceSensitive: true,
content: ["```" + lang + "\n", node.content, "\n```\n"],
Expand Down
2 changes: 1 addition & 1 deletion bbcode-src/tags/color.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { preprocessAttr, toNode } from "../utils/common";

export const color = (node) => {
const inputColor = preprocessAttr(node.attrs)._default || "";
const inputColor = preprocessAttr(node)._default || "";
if (inputColor.trim() === "") {
return node.content;
}
Expand Down
2 changes: 1 addition & 1 deletion bbcode-src/tags/div.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const div = (node, options) => {
// don't process it
return node;
}
const attrs = preprocessAttr(node.attrs);
const attrs = preprocessAttr(node, options.data.raw);
const style = attrs.style || attrs._default;
const classAttrs = attrs.class;
if (!classAttrs?.trim()) {
Expand Down
4 changes: 2 additions & 2 deletions bbcode-src/tags/divide.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { preprocessAttr, toNode } from "../utils/common";

export const divide = (node) => {
const type = (preprocessAttr(node.attrs)._default || "").toLowerCase();
const type = (preprocessAttr(node)._default || "").toLowerCase();
return toNode(
"span",
{
class: "bb-divide",
"data-type": type,
},
node.content
node.content,
);
};
4 changes: 2 additions & 2 deletions bbcode-src/tags/fieldset.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { preprocessAttr, toNode } from "../utils/common";
* @file Adds [fieldset] to bbcode
* @example [fieldset=title]content[/fieldset]
*/
export const fieldset = (node) => {
const title = preprocessAttr(node.attrs)._default || "";
export const fieldset = (node, options) => {
const title = preprocessAttr(node, options.data.raw)._default || "";
return toNode("fieldset", { class: "bb-fieldset" }, [
toNode("legend", { class: "bb-fieldset-legend" }, title),
toNode("div", { class: "bb-fieldset" }, node.content),
Expand Down
6 changes: 3 additions & 3 deletions bbcode-src/tags/font.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ const googleFontApiBuild = (family, axes) => {
};

export const font = (node, options) => {
const attrs = preprocessAttr(node.attrs);
const attrs = preprocessAttr(node, options.data.raw);
const fontFamily = attrs?._default || attrs.family || attrs.name;
if (fontFamily.trim() === "") {
return node.content;
}
if (WEB_FONTS.includes(fontFamily.trim().toLowerCase())) {
return toNode("span", { style: "font-family: " + fontFamily }, node.content);
return toNode("span", { style: `font-family: '${fontFamily}'` }, node.content);
}

const axes = axesParser(attrs);
Expand All @@ -99,7 +99,7 @@ export const font = (node, options) => {
return toNode(
"span",
{
style: `font-family: ${fontFamily}; font-weight: ${axes.wght}; font-style: ${italic}; ${fontVar}`,
style: `font-family: '${fontFamily}'; font-weight: ${axes.wght}; font-style: ${italic}; ${fontVar}`,
"data-font": url,
},
node.content,
Expand Down
4 changes: 2 additions & 2 deletions bbcode-src/tags/fontawesome.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { preprocessAttr, toNode } from "../utils/common";
import { toNode } from "../utils/common";

/**
* Adds [fa] tag
Expand All @@ -7,7 +7,7 @@ import { preprocessAttr, toNode } from "../utils/common";
* [fa primary-color="" secondary-color="" primary-opacity="" secondary-opacity="" rotate-angle=""]fa-duotone fa-icon[/fa]
*/
export const fa = (node) => {
const attrs = preprocessAttr(node.attrs);
const attrs = node.attrs;
let style = attrs.style || "";
style += attrs["primary-color"] ? `--fa-primary-color: ${attrs["primary-color"]};` : "";
style += attrs["secondary-color"] ? `--fa-secondary-color: ${attrs["secondary-color"]};` : "";
Expand Down
2 changes: 1 addition & 1 deletion bbcode-src/tags/heightrestrict.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function parseHeight(heightValue) {
* @example [heightrestrict=50]content[/heightrestrict]
*/
export const heightrestrict = (node) => {
const attrs = preprocessAttr(node.attrs)._default;
const attrs = preprocessAttr(node)._default;
const heightInput = parseHeight(attrs).toString();
// Return image's default size if heightrestrict did not involve a valid value
return heightInput === "0"
Expand Down
2 changes: 1 addition & 1 deletion bbcode-src/tags/imagefloat.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import { preprocessAttr, toNode } from "../utils/common";
* @exmaple [imagefloat=left]content[/imagefloat]
*/
export const imagefloat = (node) => {
const attrs = preprocessAttr(node.attrs)._default || "";
const attrs = preprocessAttr(node)._default || "";
return toNode("div", { class: `bb-float-${attrs}` }, node.content);
};
8 changes: 4 additions & 4 deletions bbcode-src/tags/mail.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { preprocessAttr, toNode } from "../utils/common";
import { toNode } from "../utils/common";
/**
* @file Adds [mail] to bbcode
* @param {string} [type="send"] Denotes type of mail either send or receive
Expand All @@ -23,11 +23,11 @@ const emailHeader = toNode("div", { class: "bb-email-header" }, "");
const emailFooter = toNode(
"div",
{ class: "bb-email-footer" },
toNode("div", { class: "bb-email-button" }, "")
toNode("div", { class: "bb-email-button" }, ""),
);

export const mail = (node) => {
const attributes = preprocessAttr(node.attrs);
const attributes = node.attrs;
let mailAttr = {
mailOption: (attributes.type || "send").toLowerCase(),
person: attributes.person || "Unknown",
Expand All @@ -46,6 +46,6 @@ export const mail = (node) => {
parseEmailSubject(mailAttr.subject),
parseEmailContent(node.content),
emailFooter,
]
],
);
};
2 changes: 1 addition & 1 deletion bbcode-src/tags/print.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { preprocessAttr, toNode } from "../utils/common";
*/
export const print = (node) => {
const defaultOp = "print";
const printAttr = (preprocessAttr(node.attrs)._default || defaultOp).toLowerCase();
const printAttr = (preprocessAttr(node)._default || defaultOp).toLowerCase();

const OPTIONS = ["print", "line", "graph", "parchment"];

Expand Down
2 changes: 1 addition & 1 deletion bbcode-src/tags/progress.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { preprocessAttr, toNode } from "../utils/common";
* @exmaple [progress=percentageInt]content[/progress]
*/
export const progress = (node) => {
const percentageInt = preprocessAttr(node.attrs)._default;
const percentageInt = preprocessAttr(node)._default;
return toNode("div", { class: "bb-progress" }, [
toNode("div", { class: "bb-progress-text" }, node.content),
toNode("div", { class: "bb-progress-bar", style: `width: calc(${percentageInt}% - 6px)` }, ""),
Expand Down
4 changes: 2 additions & 2 deletions bbcode-src/tags/quote.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { preprocessAttr } from "../utils/common";
/**
* rebuild the [quote] tag so that markdown-it engine can parse it for itself
*/
export const quote = (node) => {
const attrs = preprocessAttr(node.attrs);
export const quote = (node, options) => {
const attrs = preprocessAttr(node, options.data.raw);
if (node.content[0] === "\n") {
node.content.shift();
}
Expand Down
4 changes: 2 additions & 2 deletions bbcode-src/tags/rowcolumn.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { preprocessAttr, toNode } from "../utils/common";
*/
export const rowcolumn = {
row: (node) => toNode("div", { class: "bb-row" }, node.content),
column: (node) => {
const columnAttrs = preprocessAttr(node.attrs)._default || "8";
column: (node, options) => {
const columnAttrs = preprocessAttr(node, options.data.raw)._default || "8";
const columnStyle = columnAttrs.startsWith("span")
? `column-width-${columnAttrs}`
: `column-width-span${columnAttrs}`;
Expand Down
2 changes: 1 addition & 1 deletion bbcode-src/tags/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const EVENTS = [
* [script class="id" on="event" version="2"]content[/script]
*/
export const script = (node, options) => {
const attrs = preprocessAttr(node.attrs);
const attrs = preprocessAttr(node, options.data.raw);

if (!options.data.previewing && !options.data.commonGUID) {
// create a common GUID for the post
Expand Down
4 changes: 2 additions & 2 deletions bbcode-src/tags/scroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ function parseHeight(heightValue) {
* @file Adds [scroll] to bbcode
* @example [scroll]content[/scroll]
*/
export const scroll = (node) => {
const attrs = preprocessAttr(node.attrs)._default;
export const scroll = (node, options) => {
const attrs = preprocessAttr(node, options.data.raw)._default;
const heightInput = parseHeight(attrs);
return toNode("div", { class: "bb-scroll", style: `height: ${heightInput}px` }, node.content);
};
2 changes: 1 addition & 1 deletion bbcode-src/tags/side.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { preprocessAttr, toNode } from "../utils/common";

export const side = (node) => {
const attrs = preprocessAttr(node.attrs)._default || "left";
const attrs = preprocessAttr(node)._default || "left";
return toNode("div", { class: "bb-side", "data-side": attrs }, node.content);
};
2 changes: 1 addition & 1 deletion bbcode-src/tags/size.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function parseFontSize(fontValue) {
}

export const size = (node) => {
const input = preprocessAttr(node.attrs)._default;
const input = preprocessAttr(node)._default;
const fontSize = parseFontSize(input);
if (!fontSize.valid) {
return node.content;
Expand Down
Loading

0 comments on commit 81f39a7

Please sign in to comment.