Skip to content

Commit

Permalink
Merge pull request #47 from HiDeoo/hd-feat-custom-ids
Browse files Browse the repository at this point in the history
  • Loading branch information
HiDeoo authored Jul 30, 2024
2 parents 1954152 + 18b70ef commit 471000d
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 6 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ jobs:
runs-on: ${{ matrix.os }}
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Install pnpm
uses: pnpm/action-setup@v2.2.4
uses: pnpm/action-setup@v4
with:
version: 8.6.3

- name: Install Node.js
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
cache: pnpm
node-version: 18
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ jobs:
id-token: write
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Install pnpm
uses: pnpm/action-setup@v2.2.4
uses: pnpm/action-setup@v4
with:
version: 8.6.3

- name: Install Node.js
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
cache: pnpm
node-version: 18
Expand Down
5 changes: 5 additions & 0 deletions packages/starlight-links-validator/libs/remark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ export const remarkStarlightLinksValidator: Plugin<[base: string], Root> = funct
// https://github.com/syntax-tree/mdast-util-mdx-jsx#nodes
switch (node.type) {
case 'heading': {
if (node.data?.hProperties?.['id']) {
fileHeadings.push(String(node.data.hProperties['id']))
break
}

const content = toString(node)

if (content.length === 0) {
Expand Down
1 change: 1 addition & 0 deletions packages/starlight-links-validator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@types/picomatch": "2.3.3",
"astro": "4.0.4",
"mdast-util-mdx-jsx": "3.0.0",
"remark-custom-heading-id": "2.0.0",
"typescript": "5.1.3",
"unified": "11.0.4",
"vfile": "6.0.1",
Expand Down
17 changes: 17 additions & 0 deletions packages/starlight-links-validator/tests/custom-ids.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { expect, test } from 'vitest'

import { ValidationErrorType } from '../libs/validation'

import { expectValidationErrorCount, expectValidationErrors, loadFixture } from './utils'

test('should validate links with custom IDs', async () => {
expect.assertions(2)

try {
await loadFixture('custom-ids')
} catch (error) {
expectValidationErrorCount(error, 1, 1)

expectValidationErrors(error, 'test/', [['#heading-with-custom-id', ValidationErrorType.InvalidAnchor]])
}
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import starlight from '@astrojs/starlight'
import { defineConfig } from 'astro/config'
import remarkCustomHeadingId from 'remark-custom-heading-id'

import starlightLinksValidator from '../..'

export default defineConfig({
markdown: {
remarkPlugins: [remarkCustomHeadingId],
},
integrations: [
starlight({
plugins: [starlightLinksValidator()],
title: 'Starlight Links Validator Tests - custom IDs',
}),
],
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
title: Test
---

# Heading

# Heading with custom ID {#custom-id}

- [Link to heading with no custom ID](#heading)

- [Link to heading with custom ID](#custom-id)
- [Link to heading with custom ID using original ID](#heading-with-custom-id)
21 changes: 21 additions & 0 deletions pnpm-lock.yaml

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

0 comments on commit 471000d

Please sign in to comment.