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: config for anchor link generation of headings #1564

Merged
merged 9 commits into from
Oct 13, 2022
18 changes: 18 additions & 0 deletions docs/content/4.api/3.configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -334,3 +334,21 @@ Toggles the document-driven mode.
| `globals` | `Object \| Boolean` | A list of globals to be made available globally. |
| `layoutFallbacks` | `string[]` | A list of `globals` key to use to find a layout fallback. |
| `injectPage` | `boolean` | Inject `[...slug].vue` pre-configured page |

## `anchorLinks`

- Type: `Boolean | Object`{lang=ts}
- Default: `{depth: 4, exclude: [1]}`{lang=ts}

Sets if anchor links are generated with the headings.

`false`{lang=ts} will disable link generation.

`true`{lang=ts} will enable link generation for all headings.

### `anchorLinks` options

| Option | Type | Description |
| ------- | :--------: | :------------------------------------------------------- |
| depth | `number` | Sets the maximal depth for anchor link generation. |
| exclude | `number[]` | A list of which headings to exclude from link generation |
48 changes: 47 additions & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,25 @@ export interface ModuleOptions {
layoutFallbacks?: string[]
injectPage?: boolean
},
/**
* Anchor link generation config
*
* @default {}
*/
anchorLinks?: boolean | {
farnabaz marked this conversation as resolved.
Show resolved Hide resolved
/**
* Sets the maximal depth for anchor link generation.
*
* @default 4
*/
depth?: number,
/**
* Excludes headings from link generation when they are in the depth range.
*
* @default [1]
*/
exclude?: number[]
},
experimental: {
clientDB: boolean
}
Expand Down Expand Up @@ -234,6 +253,10 @@ export default defineNuxtModule<ModuleOptions>({
fields: []
},
documentDriven: false,
anchorLinks: {
depth: 4,
exclude: [1]
},
experimental: {
clientDB: false
}
Expand Down Expand Up @@ -505,6 +528,27 @@ export default defineNuxtModule<ModuleOptions>({
])
}

// Register anchor link generation
if (options.anchorLinks === true) {
options.anchorLinks = {
depth: 6,
exclude: []
}
} else if (options.anchorLinks === false) {
options.anchorLinks = {
depth: 0,
exclude: []
}
} else {
options.anchorLinks = {
...{
depth: 4,
exclude: [1]
},
...options.anchorLinks
}
}

// @ts-ignore
await nuxt.callHook('content:context', contentContext)

Expand Down Expand Up @@ -534,7 +578,9 @@ export default defineNuxtModule<ModuleOptions>({
highlight: options.highlight as any,
wsUrl: '',
// Document-driven configuration
documentDriven: options.documentDriven as ModuleOptions['documentDriven']
documentDriven: options.documentDriven as ModuleOptions['documentDriven'],
// Anchor link generation config
anchorLinks: options.anchorLinks as ModuleOptions['anchorLinks']
})

// Context will use in server
Expand Down
14 changes: 13 additions & 1 deletion src/runtime/components/Prose/ProseH1.vue
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
<template>
<h1><slot /></h1>
<h1 :id="id">
<a v-if="generate" :href="`#${id}`">
<slot />
</a>
<slot v-else />
</h1>
</template>

<script setup lang="ts">
defineProps<{ id: string }>()
const heading = 1
const { anchorLinks } = useRuntimeConfig().public.content
const generate = anchorLinks?.depth >= heading && !anchorLinks?.exclude.includes(heading)
</script>
6 changes: 5 additions & 1 deletion src/runtime/components/Prose/ProseH2.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
<template>
<h2 :id="id">
<a :href="`#${id}`">
<a v-if="generate" :href="`#${id}`">
<slot />
</a>
<slot v-else />
</h2>
</template>

<script setup lang="ts">
defineProps<{ id: string }>()
const heading = 2
const { anchorLinks } = useRuntimeConfig().public.content
const generate = anchorLinks?.depth >= heading && !anchorLinks?.exclude.includes(heading)
</script>
6 changes: 5 additions & 1 deletion src/runtime/components/Prose/ProseH3.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
<template>
<h3 :id="id">
<a :href="`#${id}`">
<a v-if="generate" :href="`#${id}`">
<slot />
</a>
<slot v-else />
</h3>
</template>

<script setup lang="ts">
defineProps<{ id: string }>()
const heading = 3
const { anchorLinks } = useRuntimeConfig().public.content
const generate = anchorLinks?.depth >= heading && !anchorLinks?.exclude.includes(heading)
</script>
6 changes: 5 additions & 1 deletion src/runtime/components/Prose/ProseH4.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
<template>
<h4 :id="id">
<a :href="`#${id}`">
<a v-if="generate" :href="`#${id}`">
<slot />
</a>
<slot v-else />
</h4>
</template>

<script setup lang="ts">
defineProps<{ id: string }>()
const heading = 4
const { anchorLinks } = useRuntimeConfig().public.content
const generate = anchorLinks?.depth >= heading && !anchorLinks?.exclude.includes(heading)
</script>
14 changes: 13 additions & 1 deletion src/runtime/components/Prose/ProseH5.vue
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
<template>
<h5><slot /></h5>
<h5 :id="id">
<a v-if="generate" :href="`#${id}`">
<slot />
</a>
<slot v-else />
</h5>
</template>

<script setup lang="ts">
defineProps<{ id: string }>()
const heading = 5
const { anchorLinks } = useRuntimeConfig().public.content
const generate = anchorLinks?.depth >= heading && !anchorLinks?.exclude.includes(heading)
</script>
14 changes: 13 additions & 1 deletion src/runtime/components/Prose/ProseH6.vue
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
<template>
<h6><slot /></h6>
<h6 :id="id">
<a v-if="generate" :href="`#${id}`">
<slot />
</a>
<slot v-else />
</h6>
</template>

<script setup lang="ts">
defineProps<{ id: string }>()
const heading = 6
const { anchorLinks } = useRuntimeConfig().public.content
const generate = anchorLinks?.depth >= heading && !anchorLinks?.exclude.includes(heading)
</script>