Skip to content

Commit

Permalink
Anotate code nodes in html with data-lang
Browse files Browse the repository at this point in the history
  • Loading branch information
zephraph committed Oct 4, 2024
1 parent 76d0c63 commit 8be58c4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
14 changes: 12 additions & 2 deletions packages/remark-obsidian/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { RemarkPlugin } from "@astrojs/markdown-remark";
import type { Node, RemarkPlugin } from "@astrojs/markdown-remark";
import { visit } from "unist-util-visit";
import internalLinkPlugin from "./internal-link";
import embedPlugin from "./embed";
import { href } from "./internal-link/utils";
import type { EmbedNode, InternalLinkNode } from "./types";
import type { CodeNode, EmbedNode, InternalLinkNode } from "./types";

interface RemarkObsidianOptions {
fileResolver?: (path: string) => Promise<string>;
Expand Down Expand Up @@ -48,6 +48,16 @@ export const remarkObsidian = (
break;
}
});

visit(root, "code", (node: CodeNode) => {
if (node.lang) {
node.data = {
hProperties: {
["data-lang"]: node.lang,
},
};
}
});
};
};

Expand Down
7 changes: 7 additions & 0 deletions packages/remark-obsidian/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ export interface EmbedNode {
data?: Record<string, any>;
}

export interface CodeNode {
type: "code";
lang?: string | null;
value?: string;
data?: Record<string, any>;
}

declare module "micromark-util-types" {
export interface TokenTypeMap extends TokenTypeMap {
internalLink: "internalLink";
Expand Down

0 comments on commit 8be58c4

Please sign in to comment.