Skip to content

Commit

Permalink
Fixed some md code timestamps being skipped
Browse files Browse the repository at this point in the history
Ran the replaceTimestamps() for `code span.token.content` on top of running a separate one for `code span.token.tag`
  • Loading branch information
Erallie committed Nov 29, 2024
1 parent 07ae4a1 commit 4c1c1eb
Showing 1 changed file with 64 additions and 60 deletions.
124 changes: 64 additions & 60 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,82 +57,81 @@ 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 = /<t:(\d{9,10}):([dDtTfFR])>/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 = /<t:(\d{9,10}):([dDtTfFR])>/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);
});

this.registerMarkdownPostProcessor((element, context) => {
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;
Expand All @@ -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({
Expand Down

0 comments on commit 4c1c1eb

Please sign in to comment.