Skip to content

Commit

Permalink
Fix notion bug
Browse files Browse the repository at this point in the history
  • Loading branch information
tmcw committed Nov 29, 2023
1 parent 0b6cf07 commit 1ede2e1
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { globby } from "globby";
import { visit } from "unist-util-visit";
import * as Fs from "node:fs/promises";
import * as Path from "node:path";
import { toString } from "mdast-util-to-string";
import meow from "meow";
import { extractTitle } from "./extract-title.js";
import { getTargetPath, linkedPath } from "./paths.js";
Expand Down Expand Up @@ -120,6 +121,49 @@ function rewriteImageUrls() {
};
}

/**
* Notion's exports have invalid Markdown syntax. If you
* have an inline code bit that is linked, the correct
* syntax is
*
* [`code`](url)
*
* But notion does
*
* `[code](url)`
*
* Bad Notion! This tries to fix this case.
*/
function fixInvertedLinkBug() {
/**
* Transform.
*
* @param {Root} tree
* Tree.
* @returns {undefined}
* Nothing.
*/
return function (tree) {
visit(tree, "inlineCode", function (node, index, parent) {
const text = toString(node);
try {
if (text.startsWith("[") && text.endsWith(")")) {
let ast = unified().use(remarkParse).parse(text);
const link = ast.children[0].children[0];
const inline = {
type: "inlineCode",
value: link.children[0].value,
};
link.children[0] = inline;
parent.children[index] = link;
}
} catch (e) {
console.error("Ran into an error fixing a link");
}
});
};
}

console.log("Transforming documents…");
for (let path of files) {
const targetPath = getTargetPath(path, INPUT_DIR, OUTPUT_DIR, ".mdx");
Expand All @@ -143,6 +187,7 @@ for (let path of files) {
.use(extractTitle)
.use(rewriteImageUrls)
.use(rewriteLinks)
.use(fixInvertedLinkBug)
.use(rewriteEmbeds);

ast = await u2.run(ast);
Expand Down

0 comments on commit 1ede2e1

Please sign in to comment.