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

feat(markdown): update default markdown-it-anchor permalink function (close #1363) #1452

Merged
merged 5 commits into from
Dec 12, 2023
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
5 changes: 5 additions & 0 deletions e2e/docs/markdown/anchors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# title

## anchor 1

### anchor 1-1
26 changes: 26 additions & 0 deletions e2e/tests/markdown/anchors.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
describe('markdown > anchors', () => {
it('should render anchors and navigate correctly', () => {
cy.visit('/markdown/anchors.html')

cy.get('.e2e-theme-content h1')
.should('have.attr', 'id', 'title')
.should('have.attr', 'tabindex', '-1')

cy.get('.e2e-theme-content h1 > a')
.should('have.attr', 'class', 'header-anchor')
.should('have.attr', 'href', '#title')
.click()

cy.location().should((location) => {
expect(location.hash).to.eq('#title')
})

cy.get('#anchor-1-1 > a')
.should('have.attr', 'class', 'header-anchor')
.click()

cy.location().should((location) => {
expect(location.hash).to.eq('#anchor-1-1')
})
})
})
7 changes: 3 additions & 4 deletions packages/markdown/src/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,10 @@ export const createMarkdown = ({
md.use<AnchorPluginOptions>(anchorPlugin, {
level: [1, 2, 3, 4, 5, 6],
slugify,
permalink: anchorPlugin.permalink.ariaHidden({
permalink: anchorPlugin.permalink.headerLink({
class: 'header-anchor',
symbol: '#',
space: true,
placement: 'before',
// Add a span inside the link so Safari shows headings in reader view.
safariReaderFix: true,
}),
...anchor,
})
Expand Down
31 changes: 17 additions & 14 deletions packages/markdown/tests/markdown.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,25 @@ import { describe, expect, it } from 'vitest'
import { createMarkdown } from '../src/index.js'

describe('@vuepress/markdown > markdown', () => {
describe('options', () => {
it.todo('anchor')
const md = createMarkdown()

it.todo('emoji')

it.todo('links')
it('should render anchors', () => {
const rendered = md.render(`\
# h1
## h2
### h3
`)
expect(rendered).toEqual(
[
'<h1 id="h1" tabindex="-1"><a class="header-anchor" href="#h1"><span>h1</span></a></h1>',
'<h2 id="h2" tabindex="-1"><a class="header-anchor" href="#h2"><span>h2</span></a></h2>',
'<h3 id="h3" tabindex="-1"><a class="header-anchor" href="#h3"><span>h3</span></a></h3>',
].join('\n') + '\n',
)
})

describe('e2e', () => {
const md = createMarkdown()

it.todo('anchor')

it('should parse emoji', () => {
const rendered = md.render(':smile:')
expect(rendered).toBe('<p>😄</p>\n')
})
it('should parse emoji', () => {
const rendered = md.render(':smile:')
expect(rendered).toBe('<p>😄</p>\n')
})
})