From a10a4300d7685bd8513faf9702f15e4d79570ea1 Mon Sep 17 00:00:00 2001 From: FelisDiligens <47528453+FelisDiligens@users.noreply.github.com> Date: Sat, 15 Jul 2023 13:40:34 +0200 Subject: [PATCH] Workaround an issue that would prevent the plugin from being loaded. --- src/cmPlugin.ts | 2 +- src/manifest.json | 2 +- src/tableUtils.ts | 67 +++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 67 insertions(+), 4 deletions(-) diff --git a/src/cmPlugin.ts b/src/cmPlugin.ts index 2889871..e71e5d1 100644 --- a/src/cmPlugin.ts +++ b/src/cmPlugin.ts @@ -1,5 +1,5 @@ -import { Table, TextAlignment } from "@felisdiligens/md-table-tools"; import { Editor } from "codemirror"; +import { Table, TextAlignment } from "@felisdiligens/md-table-tools"; import { createPosition, getColumnRanges, getRangeOfTable, isCursorInTable, replaceAllTablesFunc, replaceRange, replaceRangeFunc, replaceSelectionFunc } from "./cmUtils"; import { getCSVRenderer, getHTMLRenderer, getMarkdownParser, getMarkdownRenderer, parseTable } from "./tableUtils"; diff --git a/src/manifest.json b/src/manifest.json index 57d698a..eb86ace 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 1, "id": "joplin.plugin.MultiMarkdownTableTools", - "app_min_version": "2.12", + "app_min_version": "2.11", "version": "1.1.0", "name": "MultiMarkdown Table Tools", "description": "A collection of tools to make editing tables easier. Also supports GitHub Flavored Markdown!", diff --git a/src/tableUtils.ts b/src/tableUtils.ts index 5cd34d3..71084a4 100644 --- a/src/tableUtils.ts +++ b/src/tableUtils.ts @@ -4,6 +4,7 @@ import { GitHubFlavoredMarkdownTableParser, GitHubFlavoredMarkdownTableRenderer, HTMLTableParser, + HTMLTableParserMode, HTMLTableRenderer, MinifiedMultiMarkdownTableRenderer, MultiMarkdownTableParser, @@ -12,6 +13,7 @@ import { TableParser, TableRenderer, } from "@felisdiligens/md-table-tools"; +import TurndownService from "turndown"; const multimdParser = new MultiMarkdownTableParser(); const multimdMinifiedRenderer = new MinifiedMultiMarkdownTableRenderer(); @@ -21,7 +23,68 @@ const gfmParser = new GitHubFlavoredMarkdownTableParser(); const gfmMinifiedRenderer = new GitHubFlavoredMarkdownTableRenderer(false); const gfmPrettyRenderer = new GitHubFlavoredMarkdownTableRenderer(true); -const htmlParser = new HTMLTableParser(); +// Copied from "node_modules/@felisdiligens/md-table-tools/src/tables/common.ts": +// (see comment below for context) +/** + * Returns a TurndownService object configured for my own taste... + * (of course, if you don't like it, you can configure it to fit your needs) + */ +export function getTurndownService(): TurndownService { + const turndownService = new TurndownService({ + headingStyle: "atx", + hr: "---", + bulletListMarker: "-", + codeBlockStyle: "fenced", + fence: "```", + emDelimiter: "*", + strongDelimiter: "**", + linkStyle: "inlined", + linkReferenceStyle: "full", + }); + + // Add strikethrough: + turndownService.addRule("strikethrough", { + filter: ["del", "s"], // , 'strike' + replacement: function (content) { + return "~~" + content + "~~"; + }, + }); + + // Filter table tags: + turndownService + .remove("table") + .remove("tbody") + .remove("thead") + .remove("tr") + .remove("td") + .remove("th"); + + return turndownService; +} + +/* + I had to debug this for so long... + For whatever reason, Webpack breaks the "getTurndownService()" function from "md-table-tools". + It just throws this error: + + TypeError: a is not a constructor + at file:///home/[redacted]/.config/joplin-desktop/tmp/plugin_joplin.plugin.MultiMarkdownTableTools.js:2:656556 + at new t.HTMLTableParser (file:///home/[redacted]/.config/joplin-desktop/tmp/plugin_joplin.plugin.MultiMarkdownTableTools.js:2:656922) + at 3404 (file:///home/[redacted]/.config/joplin-desktop/tmp/plugin_joplin.plugin.MultiMarkdownTableTools.js:2:631424) + at __webpack_require__(file:///home/[redacted]/.config/joplin-desktop/tmp/plugin_joplin.plugin.MultiMarkdownTableTools.js:2:816418) + at 5049 (file:///home/[redacted]/.config/joplin-desktop/tmp/plugin_joplin.plugin.MultiMarkdownTableTools.js:2:611715) + at __webpack_require__(file:///home/[redacted]/.config/joplin-desktop/tmp/plugin_joplin.plugin.MultiMarkdownTableTools.js:2:816418) + at 3607 (file:///home/[redacted]/.config/joplin-desktop/tmp/plugin_joplin.plugin.MultiMarkdownTableTools.js:2:626211) + at __webpack_require__(file:///home/[redacted]/.config/joplin-desktop/tmp/plugin_joplin.plugin.MultiMarkdownTableTools.js:2:816418) + at file:///home/[redacted]/.config/joplin-desktop/tmp/plugin_joplin.plugin.MultiMarkdownTableTools.js:2:816911 + at file:///home/[redacted]/.config/joplin-desktop/tmp/plugin_joplin.plugin.MultiMarkdownTableTools.js:2:816938 + + It seems the instantiation in the line "const turndownService = new TurndownService({...});" in particular is broken. + As a workaround, I pass my own TurndownService object to the HTMLTableParser constructor. This seems to fix it. + Just... why? +*/ +// const htmlParser = new HTMLTableParser(); +const htmlParser = new HTMLTableParser(HTMLTableParserMode.ConvertHTMLElements, getTurndownService()); const htmlRenderer = new HTMLTableRenderer(); const csvParser = new CSVTableParser(); @@ -77,4 +140,4 @@ export function parseTable(table: string, format: string): Table { else if (table.match(/(.*,)+.*/)) return csvParser.parse(table); return null; -} \ No newline at end of file +}