Skip to content

Commit 12fc2a3

Browse files
authored
Adds support for url embeds for message and tab tags (#146)
* Adds conditional parsing for [message] tag. * Adds optional chaining to avoid crashing if a message option isn't set. * Updates message and text message parsing logic for link embeds. * Adds html embed support to tab(s) tags. * Adds optional chaining for tab tag. * Adds util function to convert input to raw tag.
1 parent 10909eb commit 12fc2a3

5 files changed

Lines changed: 48 additions & 30 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/tags/tabs.js

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,31 @@
11
import { isTagNode } from "@bbob/plugin-helper";
2-
import {
3-
generateGUID,
4-
preprocessAttr,
5-
toNode,
6-
toOriginalEndTag,
7-
toOriginalStartTag,
8-
} from "../utils/common";
2+
import { generateGUID, preprocessAttr, toNode, toRawTag } from "../utils/common";
93

104
/**
115
* @file Adds [tabs][tab] to bbcode
126
* @example [tabs][tab=name 1]content[/tab][tab=name 2]content[/tab][/tabs]
137
*/
148
export const tabs = (node, options) => {
159
const tabsList = node.content.filter(
16-
(contentNode) => isTagNode(contentNode) && contentNode.tag === "tab",
10+
(contentNode) => isTagNode(contentNode) && contentNode.tag === "tab"
1711
);
1812
const groupId = generateGUID();
1913
tabsList.forEach((tabNode) => {
2014
tabNode.isValid = true;
2115
tabNode.groupId = groupId;
2216
});
17+
2318
if (!tabsList.length) {
24-
// no [tab] tags found
25-
return [
26-
toOriginalStartTag(node, options.data.raw),
27-
...node.content,
28-
toOriginalEndTag(node, options.data.raw),
29-
];
19+
return toRawTag(node, options.data.raw);
3020
}
31-
tabsList[0].open = true;
3221

22+
tabsList[0].open = true;
3323
return toNode(
3424
"div",
3525
{
3626
class: "bb-tabs",
3727
},
38-
tabsList,
28+
tabsList
3929
);
4030
};
4131

@@ -45,15 +35,11 @@ export const tabs = (node, options) => {
4535
*/
4636
export const tab = (node, options) => {
4737
if (!node.isValid) {
48-
// not inside a [tabs] tag
49-
return [
50-
toOriginalStartTag(node, options.data.raw),
51-
...node.content,
52-
toOriginalEndTag(node, options.data.raw),
53-
];
38+
return toRawTag(node, options.data.raw);
5439
}
40+
5541
const attrs = preprocessAttr(node, options.data.raw);
56-
const name = attrs._default || attrs.name || "Tab";
42+
const name = attrs?._default || attrs?.name || "Tab";
5743
const tabId = `tab-${name.replace(/\W/g, "_")}-${generateGUID()}`;
5844
return [
5945
toNode("input", {
@@ -70,14 +56,14 @@ export const tab = (node, options) => {
7056
for: tabId,
7157
style: attrs.style,
7258
},
73-
name,
59+
name
7460
),
7561
toNode(
7662
"div",
7763
{
7864
class: "bb-tab-content",
7965
},
80-
node.content,
66+
node.content
8167
),
8268
];
8369
};

bbcode-src/tags/textmessage.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
1-
import { preprocessAttr, toNode } from "../utils/common";
1+
import { isTagNode } from "@bbob/plugin-helper";
2+
import { preprocessAttr, toNode, toRawTag } from "../utils/common";
23

34
/**
45
* @file Adds textmessage to bbcode
5-
* @exmaple [textmessage=Recipient][message=them]Hi [/message][message=me] Hey![/message][/textmessage]
6+
* @example [textmessage=Recipient][message=them]Hi [/message][message=me] Hey![/message][/textmessage]
67
*/
78

89
const ACCEPTED_OPTIONS = ["me", "them", "right", "left"];
910
export const textmessage = {
1011
textmessage: (node, options) => {
12+
const messageList = node.content.filter(
13+
(contentNode) => isTagNode(contentNode) && contentNode.tag === "message"
14+
);
15+
messageList.forEach((messageNode) => {
16+
messageNode.isValid = true;
17+
});
18+
19+
if (!messageList.length) {
20+
return toRawTag(node, options.data.raw);
21+
}
22+
1123
const attr = preprocessAttr(node, options.data.raw)._default || "Recipient";
1224
const recipient = attr && attr.trim() !== "" ? attr : "Recipient";
1325
return toNode("div", { class: "bb-textmessage" }, [
@@ -18,7 +30,11 @@ export const textmessage = {
1830
]);
1931
},
2032
message: (node, options) => {
21-
let option = preprocessAttr(node, options.data.raw)._default.toLowerCase();
33+
if (!node.isValid) {
34+
return toRawTag(node, options.data.raw);
35+
}
36+
37+
let option = preprocessAttr(node, options?.data?.raw)?._default?.toLowerCase();
2238
if (!ACCEPTED_OPTIONS.includes(option) || option === "right") {
2339
option = "me";
2440
}

bbcode-src/utils/common.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,21 @@ const toOriginalEndTag = (node, raw) => {
9696
return node.toTagEnd();
9797
};
9898

99+
/**
100+
* Attempts to convert a node to a Markdown URL-embed-safe raw tag.
101+
*
102+
* Depending on the input node, the result is either:
103+
* - A single raw tag string if no end node is present
104+
* - Or an array of such strings if an end node is present
105+
* @returns {(string|string[])} - A Markdown URL-embed-safe raw tag or an array of tags.
106+
*/
107+
const toRawTag = (node, raw) => {
108+
if (node.end) {
109+
return [toOriginalStartTag(node, raw), ...node.content, toOriginalEndTag(node, raw)];
110+
}
111+
return toOriginalStartTag(node, raw);
112+
};
113+
99114
/**
100115
* Given a string, find the first position of a regex match
101116
* @param {string} string to test against
@@ -149,6 +164,7 @@ export {
149164
toNode,
150165
toOriginalStartTag,
151166
toOriginalEndTag,
167+
toRawTag,
152168
generateGUID,
153169
preprocessAttr,
154170
regexIndexOf,

0 commit comments

Comments
 (0)