Skip to content

Commit

Permalink
default lang config
Browse files Browse the repository at this point in the history
  • Loading branch information
FranciscoMoretti committed Jan 11, 2025
1 parent ad8a832 commit b67c1df
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/hungry-cars-hammer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"mdcast": patch
---

default code language code blocks
1 change: 1 addition & 0 deletions mdcast.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
13 changes: 13 additions & 0 deletions src/clients/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions src/clients/medium.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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-", "");
Expand All @@ -143,7 +143,9 @@ class MediumClient {
}
}
}

if (!lang) {
return;
}
node.properties = {
"data-code-block-mode": "2",
"data-code-block-lang": lang,
Expand Down
1 change: 1 addition & 0 deletions src/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(""),
Expand Down

0 comments on commit b67c1df

Please sign in to comment.