Skip to content

Commit

Permalink
Fix(extractor): Correctly format LaTeX (KaTeX) equations in export (#192
Browse files Browse the repository at this point in the history
)

* fix: format KaTeX in ChatGPT export (formulas between $ or $$)

* fix(rules): remove KaTeX duplication in ChatGPT export

* fix(rules): trim() KaTeX equations

* fix: format KaTeX in Perplexity export

* fix: format KaTeX in Phind export

* docs: update notes

* chore: version 3.4.0

* docs: update notes
  • Loading branch information
Hugo-COLLIN authored Oct 20, 2024
1 parent 404e3eb commit 16a2804
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "save-my-chatbot",
"version": "3.2.6",
"version": "3.4.0",
"license": "RMD-C v1.0 License",
"author": "Hugo COLLIN",
"homepage": "https://save.hugocollin.com",
Expand Down
12 changes: 8 additions & 4 deletions public/files/updateNotes.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# Update notes
# 3.3.0
🐞 Facilitating bug reports
When an error occurs during the export process, a modal will appear inviting you report the bug.
Plus, an option is available in the icon context menu.
# 3.4.0
🔢 Export math equations
LaTeX equations from Perplexity, ChatGPT and Phind are now correctly formatted!
Math lovers, enjoy!

🐞 Facilitating bug reports (or feature requests 🚀)
When an error occurs during the export process, a modal will invite you to report the bug.
Plus, a new option is available in the icon context menu.

# 3.2.6
🧩 Some bug fixes
Expand Down
4 changes: 4 additions & 0 deletions src/scripts/content/export/extractor/domains/ChatGPT.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
"formatTables": {
"filter": "filter_formatTables",
"replacement": "replacement_formatTables"
},
"formatKatex": {
"filter": "filter_formatKatex",
"replacement": "replacement_formatKatex"
}
}
},
Expand Down
4 changes: 4 additions & 0 deletions src/scripts/content/export/extractor/domains/Perplexity.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
"formatTables": {
"filter": "filter_formatTables",
"replacement": "replacement_formatTables"
},
"formatKatex": {
"filter": "filter_formatKatex",
"replacement": "replacement_formatKatex"
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
"formatTables": {
"filter": "filter_formatTables",
"replacement": "replacement_formatTables"
},
"formatKatex": {
"filter": "filter_formatKatex",
"replacement": "replacement_formatKatex"
}
}
},
Expand Down
4 changes: 4 additions & 0 deletions src/scripts/content/export/extractor/domains/PhindChat.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
"formatTables": {
"filter": "filter_formatTables",
"replacement": "replacement_formatTables"
},
"formatKatex": {
"filter": "filter_formatKatex",
"replacement": "replacement_formatKatex"
}
}
},
Expand Down
4 changes: 4 additions & 0 deletions src/scripts/content/export/extractor/domains/PhindSearch.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
"formatTables": {
"filter": "filter_formatTables",
"replacement": "replacement_formatTables"
},
"formatKatex": {
"filter": "filter_formatKatex",
"replacement": "replacement_formatKatex"
}
}
},
Expand Down
26 changes: 26 additions & 0 deletions src/scripts/content/export/extractor/rules/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,32 @@ export function replacement_formatBeforeLinks(content, node) {
return content.trim();
}

/*
--- Format KateX ---
*/

export function filter_formatKatex(node) {
return node.nodeName === 'SPAN' && node.classList.contains('katex');
}

export function replacement_formatKatex(content, node) {
// Cannot just select "math annotation" as it's missing in the node children (but rendered in the node.textContent !?!?)
const mrow = node.querySelector("math mrow")?.textContent ?? "";
const htmlMath = node.querySelector(".katex-html")?.textContent ?? "";

// Remove mrow at start and htmlMath at end of node.textContent
const mathml = node.textContent
?.replace(mrow, "")
.replace(htmlMath, "")
.trim()
?? node.textContent;

if (node.parentNode?.classList.contains('katex-display')) {
return '\n$$' + mathml + '$$\n';
}
return '$' + mathml + '$';
}

/*
--- Phind rules ---
*/
Expand Down

0 comments on commit 16a2804

Please sign in to comment.