Skip to content

Commit 09f78c0

Browse files
committed
Refactor: move HTML->Markdown conversion to Rust from TypeScript.
1 parent 0a7a028 commit 09f78c0

File tree

13 files changed

+144
-1658
lines changed

13 files changed

+144
-1658
lines changed

client/src/CodeChatEditor-test.mts

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import {
4242
// Nothing needed at present.
4343
//
4444
// Provide convenient access to all functions tested here.
45-
const { codechat_html_to_markdown } = exportedForTesting;
45+
const {} = exportedForTesting;
4646

4747
// From [SO](https://stackoverflow.com/a/39914235).
4848
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
@@ -74,55 +74,6 @@ window.CodeChatEditor_test = () => {
7474
// Define some tests. See the [Mocha TDD docs](https://mochajs.org/#tdd) and
7575
// the [Chai assert API](https://www.chaijs.com/api/assert/).
7676
suite("CodeChatEditor.mts", function () {
77-
suite("codechat_html_to_markdown", function () {
78-
test("Translate an empty comment", async function () {
79-
const db: [CodeMirrorDocBlockTuple] = [[0, 0, "", "//", ""]];
80-
codechat_html_to_markdown(db);
81-
assert.deepEqual(db, [[0, 0, "", "//", "\n"]]);
82-
});
83-
84-
test("Translate non-breaking space", async function () {
85-
const db: [CodeMirrorDocBlockTuple] = [
86-
[0, 0, "", "//", " "],
87-
];
88-
codechat_html_to_markdown(db);
89-
assert.deepEqual(db, [[0, 0, "", "//", "\n"]]);
90-
});
91-
92-
test("Translate two empty comments", async function () {
93-
const db: CodeMirrorDocBlockTuple[] = [
94-
[0, 0, "", "//", ""],
95-
[2, 2, "", "//", ""],
96-
];
97-
const source = {
98-
doc_blocks: db,
99-
};
100-
codechat_html_to_markdown(db);
101-
assert.deepEqual(db, [
102-
[0, 0, "", "//", "\n"],
103-
[2, 2, "", "//", "\n"],
104-
]);
105-
});
106-
107-
test("Translate unclosed HTML", async function () {
108-
const db: CodeMirrorDocBlockTuple[] = [
109-
[0, 0, "", "//", "<h1><u>A<u></h1>\n"],
110-
[2, 2, "", "//", "<h2>Ax</h2>"],
111-
];
112-
codechat_html_to_markdown(db);
113-
assert.deepEqual(db, [
114-
[
115-
0,
116-
0,
117-
"",
118-
"//",
119-
"<u>A<u></u></u>\n===============\n\n<u><u></u></u>\n",
120-
],
121-
[2, 2, "", "//", "Ax\n--\n"],
122-
]);
123-
});
124-
});
125-
12677
suite("CodeMirror checks", function () {
12778
test("insert/delete/replace expectations", function () {
12879
// Create a div to hold an editor.

client/src/CodeChatEditor.mts

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@
4444
// ### JavaScript/TypeScript
4545
//
4646
// #### Third-party
47-
import TurndownService from "./third-party/turndown/turndown.browser.es.mjs";
48-
import { gfm } from "./third-party/turndown/turndown-plugin-gfm.browser.es.js";
4947
import "./third-party/wc-mermaid/wc-mermaid";
5048

5149
// #### Local
@@ -153,22 +151,6 @@ let current_metadata: {
153151
// True if the document is dirty (needs saving).
154152
let is_dirty = false;
155153

156-
// ### Markdown to HTML conversion
157-
//
158-
// Instantiate[turndown](https://github.com/mixmark-io/turndown) for HTML to
159-
// Markdown conversion
160-
const turndownService = new TurndownService({
161-
br: "\\",
162-
codeBlockStyle: "fenced",
163-
renderAsPure: false,
164-
wordWrap: [80, 40],
165-
});
166-
167-
// Add the plugins
168-
// from[turndown-plugin-gfm](https://github.com/laurent22/joplin/tree/dev/packages/turndown-plugin-gfm)
169-
// to enable conversions for tables, task lists, and strikethroughs.
170-
turndownService.use(gfm);
171-
172154
// Page initialization
173155
// -------------------
174156
export const set_is_dirty = (value: boolean = true) => {
@@ -351,15 +333,14 @@ const save_lp = (is_dirty: boolean) => {
351333
Plain: CodeMirror;
352334
}
353335
).Plain = {
354-
doc: turndownService.turndown(html),
336+
doc: html,
355337
doc_blocks: [],
356338
};
357339
// Retypeset all math after saving the document.
358340
mathJaxTypeset(codechat_body);
359341
} else {
360342
code_mirror_diffable = CodeMirror_save();
361343
assert("Plain" in code_mirror_diffable);
362-
codechat_html_to_markdown(code_mirror_diffable.Plain.doc_blocks);
363344
}
364345
update.contents = {
365346
metadata: current_metadata,
@@ -396,22 +377,6 @@ const on_save = async (only_if_dirty: boolean = false) => {
396377
is_dirty = false;
397378
};
398379

399-
const codechat_html_to_markdown = (doc_blocks: CodeMirrorDocBlockTuple[]) => {
400-
const entries = doc_blocks.entries();
401-
for (const [index, doc_block] of entries) {
402-
const wordWrapMargin = Math.max(
403-
40,
404-
80 - doc_block[3].length - doc_block[2].length - 1,
405-
);
406-
turndownService.options["wordWrap"] = [wordWrapMargin, 40];
407-
doc_block[4] =
408-
(index == doc_blocks.length - 1
409-
? turndownService.last(doc_block[4])
410-
: turndownService.next(doc_block[4])) + "\n";
411-
}
412-
turndownService.options["wordWrap"] = [80, 40];
413-
};
414-
415380
// ### Autosave feature
416381
//
417382
// Schedule an autosave; call this whenever the document is modified.
@@ -589,6 +554,4 @@ on_dom_content_loaded(async () => {
589554
// wrap all testing exports in a single variable. This avoids namespace
590555
// pollution, since only one name is exported, and it's clearly marked for
591556
// testing only. Test code still gets access to everything it needs.
592-
export const exportedForTesting = {
593-
codechat_html_to_markdown,
594-
};
557+
export const exportedForTesting = {};

client/src/third-party/turndown/turndown-plugin-gfm.browser.es.js

Lines changed: 0 additions & 158 deletions
This file was deleted.

client/src/third-party/turndown/turndown.browser.es.d.mts

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)