-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
-
23
-
24
-
25
-
26
-
27
-
28
-
29
-
30
// SPDX-FileCopyrightText: 2024 Shota FUJI <pockawoooh@gmail.com>
//
// SPDX-License-Identifier: Apache-2.0
import type * as Hast from "../../deps/npm/hast/types.ts";
import { raw } from "../../deps/npm/hast-util-raw/mod.ts";
import { remove } from "../../deps/npm/unist-util-remove/mod.ts";
const stripTags = ["script", "style", "title"];
function isElement(node: Hast.Node): node is Hast.Element {
return node.type === "element";
}
/**
* This function mutates Hast tree to somewhat align to Obsidian's HTML handling
* inside Markdown document. This function does not guarantee or aim to 100% compatible
* as Obsidian does not publish formal spec of their Markdown. Also their implementation
* is quite buggy to the degree it's almost impossible to write compatible processor.
*/
export function ofmHtml(tree: Hast.Nodes): Hast.Nodes {
const converted = raw(tree);
// Using bare unist utility, as hast-util-sanitize cannot blacklist elements
remove(converted, (node) => {
return isElement(node) && stripTags.includes(node.tagName);
});
return converted;
}