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

fix(a11y)!: use github-style header anchor generation #4609

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
62 changes: 34 additions & 28 deletions src/client/theme-default/styles/components/vp-doc.css
Original file line number Diff line number Diff line change
@@ -2,6 +2,10 @@
* Headings
* -------------------------------------------------------------------------- */

.vp-doc .header-wrapper {
position: relative;
}

.vp-doc h1,
.vp-doc h2,
.vp-doc h3,
@@ -15,29 +19,56 @@

.vp-doc h1 {
letter-spacing: -0.02em;
}

.vp-doc h1,
.vp-doc h1 + .header-anchor {
line-height: 40px;
font-size: 28px;
}

@media (min-width: 768px) {
.vp-doc h1,
.vp-doc h1 + .header-anchor {
font-size: 32px;
}
}

.vp-doc h2 {
margin: 48px 0 16px;
border-top: 1px solid var(--vp-c-divider);
padding-top: 24px;
letter-spacing: -0.02em;
}

.vp-doc h2,
.vp-doc h2 + .header-anchor {
line-height: 32px;
font-size: 24px;
}

.vp-doc h2 + .header-anchor {
top: 24px;
}

.vp-doc h3 {
margin: 32px 0 0;
letter-spacing: -0.01em;
}

.vp-doc h3,
.vp-doc h3 + .header-anchor {
line-height: 28px;
font-size: 20px;
}

.vp-doc h4 {
margin: 24px 0 0;
letter-spacing: -0.01em;
}

.vp-doc h4,
.vp-doc h4 + .header-anchor {
line-height: 24px;
font-size: 18px;
}
@@ -47,6 +78,7 @@
top: 0;
left: 0;
margin-left: -0.87em;
padding-right: 0.3em;
font-weight: 500;
user-select: none;
opacity: 0;
@@ -56,37 +88,11 @@
opacity 0.25s;
}

.vp-doc .header-anchor:before {
content: var(--vp-header-anchor-symbol);
}

.vp-doc h1:hover .header-anchor,
.vp-doc h1 .header-anchor:focus,
.vp-doc h2:hover .header-anchor,
.vp-doc h2 .header-anchor:focus,
.vp-doc h3:hover .header-anchor,
.vp-doc h3 .header-anchor:focus,
.vp-doc h4:hover .header-anchor,
.vp-doc h4 .header-anchor:focus,
.vp-doc h5:hover .header-anchor,
.vp-doc h5 .header-anchor:focus,
.vp-doc h6:hover .header-anchor,
.vp-doc h6 .header-anchor:focus {
.vp-doc .header-wrapper:hover .header-anchor,
.vp-doc .header-wrapper .header-anchor:focus {
opacity: 1;
}

@media (min-width: 768px) {
.vp-doc h1 {
letter-spacing: -0.02em;
line-height: 40px;
font-size: 32px;
}
}

.vp-doc h2 .header-anchor {
top: 24px;
}

/**
* Paragraph and inline elements
* -------------------------------------------------------------------------- */
8 changes: 5 additions & 3 deletions src/client/theme-default/styles/utils.css
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
.visually-hidden {
position: absolute;
overflow: hidden;
clip: rect(0, 0, 0, 0);
width: 1px;
height: 1px;
margin: -1px;
padding: 0;
border-width: 0;
white-space: nowrap;
clip: rect(0 0 0 0);
clip-path: inset(50%);
overflow: hidden;
}
8 changes: 0 additions & 8 deletions src/client/theme-default/styles/vars.css
Original file line number Diff line number Diff line change
@@ -316,14 +316,6 @@
--vp-layout-max-width: 1440px;
}

/**
* Component: Header Anchor
* -------------------------------------------------------------------------- */

:root {
--vp-header-anchor-symbol: '#';
}

/**
* Component: Code
* -------------------------------------------------------------------------- */
30 changes: 5 additions & 25 deletions src/node/markdown/markdown.ts
Original file line number Diff line number Diff line change
@@ -274,31 +274,11 @@ export async function createMarkdownRenderer(
.map((t) => t.content)
.join('')
},
permalink: (slug, _, state, idx) => {
const title =
state.tokens[idx + 1]?.children
?.filter((token) => ['text', 'code_inline'].includes(token.type))
.reduce((acc, t) => acc + t.content, '')
.trim() || ''

const linkTokens = [
Object.assign(new state.Token('text', '', 0), { content: ' ' }),
Object.assign(new state.Token('link_open', 'a', 1), {
attrs: [
['class', 'header-anchor'],
['href', `#${slug}`],
['aria-label', `Permalink to “${title}”`]
]
}),
Object.assign(new state.Token('html_inline', '', 0), {
content: '​',
meta: { isPermalinkSymbol: true }
}),
new state.Token('link_close', 'a', -1)
]

state.tokens[idx + 1].children?.push(...linkTokens)
},
permalink: anchorPlugin.permalink.linkAfterHeader({
assistiveText: (title) => `Permalink to “${title.trim()}”`,
visuallyHiddenClass: 'visually-hidden',
wrapper: ['<div class="header-wrapper">', '</div>']
}),
...options.anchor
} as anchorPlugin.AnchorOptions).use(frontmatterPlugin, {
...options.frontmatter
Loading
Oops, something went wrong.