Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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.

36 changes: 11 additions & 25 deletions bbcode-src/tags/tabs.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,31 @@
import { isTagNode } from "@bbob/plugin-helper";
import {
generateGUID,
preprocessAttr,
toNode,
toOriginalEndTag,
toOriginalStartTag,
} from "../utils/common";
import { generateGUID, preprocessAttr, toNode, toRawTag } from "../utils/common";

/**
* @file Adds [tabs][tab] to bbcode
* @example [tabs][tab=name 1]content[/tab][tab=name 2]content[/tab][/tabs]
*/
export const tabs = (node, options) => {
const tabsList = node.content.filter(
(contentNode) => isTagNode(contentNode) && contentNode.tag === "tab",
(contentNode) => isTagNode(contentNode) && contentNode.tag === "tab"
);
const groupId = generateGUID();
tabsList.forEach((tabNode) => {
tabNode.isValid = true;
tabNode.groupId = groupId;
});

if (!tabsList.length) {
// no [tab] tags found
return [
toOriginalStartTag(node, options.data.raw),
...node.content,
toOriginalEndTag(node, options.data.raw),
];
return toRawTag(node, options.data.raw);
}
tabsList[0].open = true;

tabsList[0].open = true;
return toNode(
"div",
{
class: "bb-tabs",
},
tabsList,
tabsList
);
};

Expand All @@ -45,15 +35,11 @@ export const tabs = (node, options) => {
*/
export const tab = (node, options) => {
if (!node.isValid) {
// not inside a [tabs] tag
return [
toOriginalStartTag(node, options.data.raw),
...node.content,
toOriginalEndTag(node, options.data.raw),
];
return toRawTag(node, options.data.raw);
}

const attrs = preprocessAttr(node, options.data.raw);
const name = attrs._default || attrs.name || "Tab";
const name = attrs?._default || attrs?.name || "Tab";
const tabId = `tab-${name.replace(/\W/g, "_")}-${generateGUID()}`;
return [
toNode("input", {
Expand All @@ -70,14 +56,14 @@ export const tab = (node, options) => {
for: tabId,
style: attrs.style,
},
name,
name
),
toNode(
"div",
{
class: "bb-tab-content",
},
node.content,
node.content
),
];
};
22 changes: 19 additions & 3 deletions bbcode-src/tags/textmessage.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
import { preprocessAttr, toNode } from "../utils/common";
import { isTagNode } from "@bbob/plugin-helper";
import { preprocessAttr, toNode, toRawTag } from "../utils/common";

/**
* @file Adds textmessage to bbcode
* @exmaple [textmessage=Recipient][message=them]Hi [/message][message=me] Hey![/message][/textmessage]
* @example [textmessage=Recipient][message=them]Hi [/message][message=me] Hey![/message][/textmessage]
*/

const ACCEPTED_OPTIONS = ["me", "them", "right", "left"];
export const textmessage = {
textmessage: (node, options) => {
const messageList = node.content.filter(
(contentNode) => isTagNode(contentNode) && contentNode.tag === "message"
);
messageList.forEach((messageNode) => {
messageNode.isValid = true;
});

if (!messageList.length) {
return toRawTag(node, options.data.raw);
}

const attr = preprocessAttr(node, options.data.raw)._default || "Recipient";
const recipient = attr && attr.trim() !== "" ? attr : "Recipient";
return toNode("div", { class: "bb-textmessage" }, [
Expand All @@ -18,7 +30,11 @@ export const textmessage = {
]);
},
message: (node, options) => {
let option = preprocessAttr(node, options.data.raw)._default.toLowerCase();
if (!node.isValid) {
return toRawTag(node, options.data.raw);
}

let option = preprocessAttr(node, options?.data?.raw)?._default?.toLowerCase();
if (!ACCEPTED_OPTIONS.includes(option) || option === "right") {
option = "me";
}
Expand Down
16 changes: 16 additions & 0 deletions bbcode-src/utils/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,21 @@ const toOriginalEndTag = (node, raw) => {
return node.toTagEnd();
};

/**
* Attempts to convert a node to a Markdown URL-embed-safe raw tag.
*
* Depending on the input node, the result is either:
* - A single raw tag string if no end node is present
* - Or an array of such strings if an end node is present
* @returns {(string|string[])} - A Markdown URL-embed-safe raw tag or an array of tags.
*/
const toRawTag = (node, raw) => {
if (node.end) {
return [toOriginalStartTag(node, raw), ...node.content, toOriginalEndTag(node, raw)];
}
return toOriginalStartTag(node, raw);
};

/**
* Given a string, find the first position of a regex match
* @param {string} string to test against
Expand Down Expand Up @@ -149,6 +164,7 @@ export {
toNode,
toOriginalStartTag,
toOriginalEndTag,
toRawTag,
generateGUID,
preprocessAttr,
regexIndexOf,
Expand Down