Skip to content

Commit

Permalink
Workaround an issue that would prevent the plugin from being loaded.
Browse files Browse the repository at this point in the history
  • Loading branch information
FelisDiligens committed Jul 15, 2023
1 parent bc0c743 commit a10a430
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/cmPlugin.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down
2 changes: 1 addition & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
@@ -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!",
Expand Down
67 changes: 65 additions & 2 deletions src/tableUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
GitHubFlavoredMarkdownTableParser,
GitHubFlavoredMarkdownTableRenderer,
HTMLTableParser,
HTMLTableParserMode,
HTMLTableRenderer,
MinifiedMultiMarkdownTableRenderer,
MultiMarkdownTableParser,
Expand All @@ -12,6 +13,7 @@ import {
TableParser,
TableRenderer,
} from "@felisdiligens/md-table-tools";
import TurndownService from "turndown";

const multimdParser = new MultiMarkdownTableParser();
const multimdMinifiedRenderer = new MinifiedMultiMarkdownTableRenderer();
Expand All @@ -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();
Expand Down Expand Up @@ -77,4 +140,4 @@ export function parseTable(table: string, format: string): Table {
else if (table.match(/(.*,)+.*/))
return csvParser.parse(table);
return null;
}
}

0 comments on commit a10a430

Please sign in to comment.