Skip to content

Commit

Permalink
Convert hyphen-like and dash characters to hyphens and HTML entities (f…
Browse files Browse the repository at this point in the history
…oundryvtt#10478)

Also kill the jquery
  • Loading branch information
stwlam committed Oct 2, 2023
1 parent 4a91fb7 commit d61a2c6
Showing 1 changed file with 37 additions and 22 deletions.
59 changes: 37 additions & 22 deletions build/lib/extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ declare global {
const { window } = new JSDOM();
global.document = window.document;
global.window = global.document.defaultView!;
const $ = (await import("jquery")).default;

interface ExtractArgs {
packDb: string;
Expand Down Expand Up @@ -337,13 +336,14 @@ class PackExtractor {
return "";
}

const $description = ((): JQuery => {
const container = (() => {
try {
return $(
const div = document.createElement("div");
div.innerHTML =
description.startsWith("<p>") && /<\/(?:p|ol|ul|table)>$/.test(description)
? description
: `<p>${description}</p>`
);
: `<p>${description}</p>`;
return div;
} catch (error) {
console.error(error);
throw PackError(
Expand All @@ -352,35 +352,50 @@ class PackExtractor {
}
})();

const textNodes: Text[] = [];
function pushTextNode(node: Node | null): void {
if (!node) return;
if (node.nodeName === "#text" && node.nodeValue) {
textNodes.push(node as Text);
}
node.childNodes.forEach((n) => {
pushTextNode(n);
});
}

pushTextNode(container);
for (const node of textNodes) {
node.nodeValue =
node.nodeValue
?.replace(/ {2,}/g, " ")
.replace(/\u2011/g, "-")
.trim() ?? null;
}

// Strip out span tags from AoN copypasta
const selectors = ["span#ctl00_MainContent_DetailedOutput", "span.fontstyle0"];
for (const selector of selectors) {
$description.find(selector).each((_i, span) => {
$(span)
.contents()
.unwrap(selector)
.each((_j, node) => {
if (node.nodeName === "#text") {
node.textContent = node.textContent!.trim();
}
});
container.querySelectorAll(selector).forEach((span) => {
span.replaceWith(span.innerHTML);
});
}

return $("<div>")
.append($description)
.html()
return container.innerHTML
.replace(/<([hb]r)>/g, "<$1 />") // Prefer self-closing tags
.replace(/&nbsp;/g, " ")
.replace(/ {2,}/g, " ")
.replace(/<p> ?<\/p>/g, "")
.replace(/<\/p> ?<p>/g, "</p>\n<p>")
.replace(/<p>[ \r\n]+/g, "<p>")
.replace(/[ \r\n]+<\/p>/g, "</p>")
.replace(/<(?:b|strong)>\s*/g, "<strong>")
.replace(/\s*<\/(?:b|strong)>/g, "</strong>")
.replace(/(<\/strong>)(\w)/g, "$1 $2")
.replace(/(<p><\/p>)/g, "")
.replace(/\bpf2-icon\b/g, "action-glyph")
.replace(/<p>\s*<\/p>/g, "")
.replace(/<div>\s*<\/div>/g, "")
.replace(/&nbsp;/g, " ")
.replace(/\u2013/g, "&ndash;")
.replace(/\s*\u2014\s*/g, "&mdash;")
.trim()
.replace(/^<hr \/>/, "")
.trim();
};

Expand Down Expand Up @@ -871,4 +886,4 @@ class PackExtractor {
}
}

export { type ExtractArgs, PackExtractor };
export { PackExtractor, type ExtractArgs };

0 comments on commit d61a2c6

Please sign in to comment.