diff --git a/.changeset/hungry-cars-hammer.md b/.changeset/hungry-cars-hammer.md new file mode 100644 index 0000000..3b67c39 --- /dev/null +++ b/.changeset/hungry-cars-hammer.md @@ -0,0 +1,5 @@ +--- +"mdcast": patch +--- + +default code language code blocks diff --git a/mdcast.config.ts b/mdcast.config.ts index e8a1111..610fc7a 100644 --- a/mdcast.config.ts +++ b/mdcast.config.ts @@ -14,6 +14,7 @@ const config: MdCastConfigInput = { canonical_url_base: "https://www.franciscomoretti.com/blog", link_url_base: "https://www.franciscomoretti.com", image_url_base: "https://www.franciscomoretti.com", + default_lang: "typescript", }, devto: { should_publish: true, diff --git a/src/clients/markdown.ts b/src/clients/markdown.ts index 2da7986..4fd88cf 100644 --- a/src/clients/markdown.ts +++ b/src/clients/markdown.ts @@ -95,6 +95,19 @@ class MarkdownClient { }); } ) + .use( + // Insert default lang in code blocks + () => (tree: Root) => { + visit(tree, (node: Nodes) => { + if (node.type === "code") { + if (!node.lang && this.config.default_lang) { + // Only insert default lang if it's not already set + node.lang = this.config.default_lang; + } + } + }); + } + ) .use(() => (tree: Root) => { // Remove frontmatter node from the tree const [frontmatterNode, ...restNodes]: RootContent[] = tree.children; diff --git a/src/clients/medium.ts b/src/clients/medium.ts index 4fa8cf9..7bd4ad4 100644 --- a/src/clients/medium.ts +++ b/src/clients/medium.ts @@ -130,7 +130,7 @@ class MediumClient { return; const className = codeNode.properties["className"]; - let lang = "markdown"; // TODO: get default lang from config + let lang = undefined; if (className) { if (typeof className === "string") { lang = className.replace("language-", ""); @@ -143,7 +143,9 @@ class MediumClient { } } } - + if (!lang) { + return; + } node.properties = { "data-code-block-mode": "2", "data-code-block-lang": lang, diff --git a/src/config/schema.ts b/src/config/schema.ts index eaf9fa1..96e47c9 100644 --- a/src/config/schema.ts +++ b/src/config/schema.ts @@ -72,6 +72,7 @@ const FrontMatterPropertiesSchema = z.object({ const MarkdownConfigSchema = z.object({ frontmatterProperties: FrontMatterPropertiesSchema.optional().default({}), + default_lang: z.string().optional().default(""), link_url_base: z.string().default(""), canonical_url_base: z.string().default(""), image_url_base: z.string().default(""),