Skip to content

Commit

Permalink
docs: add heading anchors (#814)
Browse files Browse the repository at this point in the history
* docs: add heading anchors

* refactor: remove hastscript from docs deps

---------

Co-authored-by: Odil Abdulloyev <abdulloyevodil@mail.ru>
  • Loading branch information
lid0a and Odil Abdulloyev authored Apr 17, 2024
1 parent 60527d9 commit 1f8e18c
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 22 deletions.
2 changes: 1 addition & 1 deletion docs/.astro/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,5 +440,5 @@ declare module 'astro:content' {

type AnyEntryMap = ContentEntryMap & DataEntryMap;

export type ContentConfig = typeof import("../src/content/config.js");
export type ContentConfig = typeof import("./../src/content/config.js");
}
19 changes: 19 additions & 0 deletions docs/astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { $ } from 'zx'
import { defineConfig } from 'astro/config'
import starlight from '@astrojs/starlight'
import rehypeExternalLinks from 'rehype-external-links'
import rehypeAutolinkHeadings from 'rehype-autolink-headings'
import rehypeSlug from 'rehype-slug'

if (!process.env.VERCEL) await $`tsx sync-readme-to-pages.ts`

// https://astro.build/config
Expand Down Expand Up @@ -100,6 +103,22 @@ export default defineConfig({
target: '_blank',
},
],
rehypeSlug,
[
rehypeAutolinkHeadings,
{
properties: { class: 'anchor-link' },
behavior: 'after',
content: () => [{ type: 'text', value: '#' }],
group: ({ tagName }) => ({
type: 'element',
tagName: 'div',
properties: {
class: `heading-wrapper level-${tagName}`,
},
}),
},
],
],
},
})
101 changes: 84 additions & 17 deletions docs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
},
"devDependencies": {
"@astrojs/starlight": "^0.21.2",
"zx": "^7.2.3",
"rehype-autolink-headings": "^7.1.0",
"rehype-external-links": "^3.0.0",
"rehype-slug": "^6.0.0",
"sharp": "^0.33.3",
"tsx": "^4.7.1",
"typescript": "^5.4.3",
"rehype-external-links": "^3.0.0",
"sharp": "^0.33.3"
"zx": "^7.2.3"
}
}
43 changes: 42 additions & 1 deletion docs/src/styles/custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
--sl-color-gray-5: #35354c;
--sl-color-gray-6: #24243a;
--sl-color-black: #171721;

/* heading anchor icon size */
--icon-size: 0.75em;
}
/* Light mode colors. */
:root[data-theme='light'] {
Expand Down Expand Up @@ -51,4 +54,42 @@ main::before {
.external-link-icon {
text-decoration: none;
display: inline-block;
}
}

.heading-wrapper > :is(h2, h3, h4, h5, h6) {
display: inline;
}

.heading-wrapper .anchor-link {
opacity: 0;
margin-inline-start: calc(var(--icon-size) / 2);
text-decoration: none;
&:hover {
text-decoration: underline;
}
}

.heading-wrapper:hover .anchor-link,
.heading-wrapper .anchor-link:focus {
opacity: 1;
}

.level-h2 {
font-size: var(--sl-text-h2);
}

.level-h3 {
font-size: var(--sl-text-h3);
}

.level-h4 {
font-size: var(--sl-text-h4);
}

.level-h5 {
font-size: var(--sl-text-h5);
}

.level-h6 {
font-size: var(--sl-text-h6);
}

0 comments on commit 1f8e18c

Please sign in to comment.