Skip to content

Commit

Permalink
fix: properly index <h2> sections
Browse files Browse the repository at this point in the history
  • Loading branch information
taskylizard committed Jan 12, 2025
1 parent 84ae282 commit d6b9979
Show file tree
Hide file tree
Showing 10 changed files with 346 additions and 294 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ TODO text
*.tmpl text
*.tpl text
*.twig text
*.vue text
*.vue text diff=vue

# Configs
*.cnf text
Expand Down
2 changes: 1 addition & 1 deletion .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ Select edit button and make your changes.

1. Fork the repository by clicking the "Fork" button in the top right corner.

2. Make sure you have [Node.js](https://nodejs.org/en/), [pnpm](https://pnpm.io/), [git](https://git-scm.com/), and [VSCode](https://code.visualstudio.com/) or any other editor installed.
2. Make sure you have [Node.js](https://nodejs.org/en/), [pnpm](https://pnpm.io/), [git](https://git-scm.com/), and [VSCode](https://code.visualstudio.com/) or any other editor installed. Alternatively, `pnpm` has `pnpm env` you can use to manage Node.

3. Clone your forked repository to your local machine.

Expand Down
1 change: 0 additions & 1 deletion .github/ISSUE_TEMPLATE/config.yml

This file was deleted.

1 change: 0 additions & 1 deletion .github/POST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ title: Monthly Updates [Month]
description: Month 20YY updates
date: 20YY-MM-DD
next: false
aside: left
prev: false
sidebar: false
footer: true
Expand Down
23 changes: 0 additions & 23 deletions .github/PULL_REQUEST_TEMPLATE.md

This file was deleted.

9 changes: 0 additions & 9 deletions .github/labeler.yml

This file was deleted.

17 changes: 0 additions & 17 deletions .github/workflows/labeler.yml

This file was deleted.

51 changes: 42 additions & 9 deletions docs/.vitepress/markdown/headers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) taskylizard. All rights reserved.
* Copyright (c) 2024 taskylizard
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -13,20 +13,53 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import type { MarkdownRenderer } from 'vitepress'
import { headers } from '../transformer/constants'

const titles = Object.keys(headers).map((key) => headers[key].title)
const excluded = ['Credits']

export const headersPlugin = (md: MarkdownRenderer) => {
// Add the Feedback component after the heading and close the container
md.renderer.rules.heading_close = (tokens, idx, options, env, self) => {
// Add the Feedback component in the heading, before the link.
//
// Adding it after the link is closed prevents vitepress from properly
// indexing the file's content.

md.renderer.rules.heading_open = (tokens, idx, options, env, self) => {
const result = self.renderToken(tokens, idx, options)
const heading = tokens[idx - 1]

const idxClose =
idx +
tokens.slice(idx).findIndex((token) => token.type === 'heading_close')
if (idxClose <= idx) return result

const level = tokens[idx].tag.slice(1)
if (!titles.includes(env.frontmatter.title) || level !== '2') return result
if (excluded.includes(env.frontmatter.title) || level !== '2') return result

// Find the token for the link.
//
// The token after `heading_open` contains the link as a child token.
const children = tokens[idx + 1].children || []
const linkOpenToken = children.find((c) => c.type === 'link_open')
if (!linkOpenToken) return result

const heading = tokens[idxClose - 1]

linkOpenToken.meta = linkOpenToken.meta || {}
linkOpenToken.meta.feedback = {
heading: heading.content
}

return result
}

md.renderer.rules.link_open = (tokens, idx, options, env, self) => {
const result = self.renderToken(tokens, idx, options)

const meta = tokens[idx].meta
if (!meta || !meta.feedback) return result

const heading = meta.feedback.heading || ''
if (!heading) return result

return `<Feedback heading="${heading.content}" />${result}`
return `<Feedback heading="${heading}" />${result}`
}
}
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"docs:dev": "vitepress dev docs/",
"docs:preview": "vitepress preview docs/",
"format": "prettier -w --cache --check .",
"postinstall": "nitropack prepare",
"lint": "biome lint .",
"lint:fix": "biome lint . --write",
"lint:fix:unsafe": "biome lint . --write --unsafe",
Expand All @@ -33,15 +32,15 @@
"nitro-cors": "^0.7.1",
"nitropack": "^2.10.4",
"nprogress": "^0.2.0",
"pathe": "^1.1.2",
"unocss": "^0.63.4",
"pathe": "^2.0.1",
"unocss": "65.4.0",
"vitepress": "^1.5.0",
"vue": "^3.5.12",
"x-satori": "^0.2.0",
"zod": "^3.23.8"
},
"devDependencies": {
"@biomejs/biome": "^1.9.3",
"@biomejs/biome": "^1.9.4",
"@cloudflare/workers-types": "^4.20241230.0",
"@ianvs/prettier-plugin-sort-imports": "^4.3.1",
"@iconify-json/carbon": "^1.2.3",
Expand Down
Loading

0 comments on commit d6b9979

Please sign in to comment.