From 4c1c1eb579fe415123ca2ca16dabe78ef0541d6b Mon Sep 17 00:00:00 2001 From: Erallie <31375589+Erallie@users.noreply.github.com> Date: Fri, 29 Nov 2024 11:29:23 -0700 Subject: [PATCH] Fixed some md code timestamps being skipped Ran the replaceTimestamps() for `code span.token.content` on top of running a separate one for `code span.token.tag` --- main.ts | 124 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 64 insertions(+), 60 deletions(-) diff --git a/main.ts b/main.ts index d58b469..cf1555f 100644 --- a/main.ts +++ b/main.ts @@ -57,74 +57,73 @@ export default class DiscordTimestamps extends Plugin { }; } - this.registerMarkdownPostProcessor((element, context) => { - - function replaceTimestamp(element: HTMLElement) { - if (element.localName == "code" && plugin.settings.codeblocks == false) { - return; + function replaceTimestamp(element: HTMLElement) { + if (element.localName == "code" && plugin.settings.codeblocks == false) { + return; + } + else if (element.nodeType == element.TEXT_NODE) { + let text = element.textContent || ""; + const originalText = text; + if (text == null || text == "") { + return element; } - else if (element.nodeType == element.TEXT_NODE) { - let text = element.textContent || ""; - const originalText = text; - if (text == null || text == "") { - return element; - } - let match; - let textSlices: string[] = []; - let timestampSlices: string[] = []; - let timestampHover: string[] = []; - while ((match = //g.exec(text)) !== null) { - let timestamp = processTimestamp(match); - - if (timestamp === null) - continue; - - textSlices.push(text.slice(0, text.indexOf(match[0]))) - text = text.slice(text.indexOf(match[0]) + match[0].length); - timestampSlices.push(timestamp.timeParsed); - timestampHover.push(timestamp.full); - } - if (text !== originalText) { - let newEl = new DocumentFragment; - for (let i = 0; i < textSlices.length; i++) { - if (i == 0) { - newEl.textContent = textSlices[i] - } - else { - newEl.appendText(textSlices[i]); - } - if (i < timestampSlices.length && i < timestampHover.length) { - let timestampEl = newEl.createEl('span', { text: timestampSlices[i], cls: 'discord-timestamps' }); - timestampEl.ariaLabel = timestampHover[i]; - timestampEl.ontouchend = (ev) => { - new Notice(timestampHover[i]); - } - } - else if (timestampSlices.length !== timestampHover.length) { - console.error("The lengths of timestampSlices and timestampHover are NOT EQUAL!"); + let match; + let textSlices: string[] = []; + let timestampSlices: string[] = []; + let timestampHover: string[] = []; + while ((match = //g.exec(text)) !== null) { + let timestamp = processTimestamp(match); + + if (timestamp === null) + continue; + + textSlices.push(text.slice(0, text.indexOf(match[0]))) + text = text.slice(text.indexOf(match[0]) + match[0].length); + timestampSlices.push(timestamp.timeParsed); + timestampHover.push(timestamp.full); + } + if (text !== originalText) { + let newEl = new DocumentFragment; + for (let i = 0; i < textSlices.length; i++) { + if (i == 0) { + newEl.textContent = textSlices[i] + } + else { + newEl.appendText(textSlices[i]); + } + if (i < timestampSlices.length && i < timestampHover.length) { + let timestampEl = newEl.createEl('span', { text: timestampSlices[i], cls: 'discord-timestamps' }); + timestampEl.ariaLabel = timestampHover[i]; + timestampEl.ontouchend = (ev) => { + new Notice(timestampHover[i]); } } - if (text !== "") { - newEl.appendText(text); + else if (timestampSlices.length !== timestampHover.length) { + console.error("The lengths of timestampSlices and timestampHover are NOT EQUAL!"); } - - element.replaceWith(newEl); } - } - else if (element.nodeType == element.ELEMENT_NODE) { - let child = element.firstChild as HTMLElement; - /* let children = Array.from(element.children); - for (let child of children) { - replaceTimestamp(child as HTMLElement) - } */ - while (child) { - const nextChild = child.nextSibling; - replaceTimestamp(child); - child = nextChild as HTMLElement; + if (text !== "") { + newEl.appendText(text); } + + element.replaceWith(newEl); + } + } + else if (element.nodeType == element.ELEMENT_NODE) { + let child = element.firstChild as HTMLElement; + /* let children = Array.from(element.children); + for (let child of children) { + replaceTimestamp(child as HTMLElement) + } */ + while (child) { + const nextChild = child.nextSibling; + replaceTimestamp(child); + child = nextChild as HTMLElement; } } + } + this.registerMarkdownPostProcessor((element, context) => { replaceTimestamp(element); }); @@ -132,7 +131,7 @@ export default class DiscordTimestamps extends Plugin { if (!plugin.settings.mdCodeblocks) return; - let elements = element.findAll('code span.token'); + let elements = element.findAll('code span.token.tag'); for (let el of elements) { let text = el.textContent; @@ -158,6 +157,11 @@ export default class DiscordTimestamps extends Plugin { el.replaceWith(newEl); } + + let elements2 = element.findAll('code span.token.content'); + for (let el of elements2) { + replaceTimestamp(el); + } }, 1000) this.addCommand({