Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blog: Add generated section "On this page" #16

Merged
merged 2 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions blog/games.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ authors:
username: "EthanThatOneKid"
topics: ["Games"]
date: 2024-06-05
toc: false
---

At FartLabs, we're passionate about developer tools, but we also love creating
Expand Down
3 changes: 3 additions & 0 deletions components/blog_page/markdown.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import MarkdownIt from "markdown-it";
import anchorPlugin from "markdown-it-anchor";
import tocDoneRightPlugin from "markdown-it-toc-done-right";
import hljs from "highlight.js";

/**
Expand Down Expand Up @@ -35,3 +36,5 @@ renderer.use(anchorPlugin, {
class: "fart-link-visible-on-hover",
}),
});

renderer.use(tocDoneRightPlugin);
9 changes: 7 additions & 2 deletions components/blog_page/posts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,16 @@ export function readPostSync(entry: WalkEntry): Post {
return {
id,
attrs: extracted.attrs,
html: renderHTML(extracted.body),
html: renderHTML(
`${(extracted.attrs.toc ?? true) ? tocPrefix : ""}${extracted.body}`,
),
};
}

const tocPrefix = "## On this page\n\n[[toc]]\n";

/**
* constTopics counts the topics.
* countTopics counts the topics.
*/
export function countTopics(posts: Post[]): Map<string, number> {
const topics = new Map<string, number>();
Expand Down Expand Up @@ -115,6 +119,7 @@ export interface PostAttrs {
topics: string[];
date: Date;
aliases?: string[];
toc?: boolean;
}

/**
Expand Down
1 change: 1 addition & 0 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"highlight.js": "npm:highlight.js@^11.9.0",
"markdown-it": "npm:markdown-it@^14.1.0",
"markdown-it-anchor": "npm:markdown-it-anchor@^9.0.0",
"markdown-it-toc-done-right": "npm:markdown-it-toc-done-right@^4.2.0",
"simplex-noise": "npm:simplex-noise@^4.0.1"
},
"tasks": {
Expand Down
Loading