Skip to content

Commit bd05fda

Browse files
committed
doc: update site anchor
1 parent 9bb6e79 commit bd05fda

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

site/src/layouts/index.vue

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
<a-anchor-link
4343
v-for="h in headers"
4444
:key="h.title"
45-
:href="h.href || `#${h.title.replace(/^(\d)/, '_$1')}`"
45+
:href="h.href || `#${slugifyTitle(h.title)}`"
4646
:title="h.title"
4747
></a-anchor-link>
4848
</a-anchor>
@@ -88,6 +88,9 @@ import RightBottomAd from '../components/rice/right_bottom_rice.vue';
8888
import { CloseOutlined, MenuOutlined } from '@ant-design/icons-vue';
8989
import ThemeIcon from './ThemeIcon.vue';
9090
91+
const rControl = /[\u0000-\u001f]/g;
92+
const rSpecial = /[\s~`!@#$%^&*()\-_+=[\]{}|\\;:"'<>,.?/]+/g;
93+
9194
export default defineComponent({
9295
name: 'Layout',
9396
components: {
@@ -162,6 +165,21 @@ export default defineComponent({
162165
visible.value = !visible.value;
163166
};
164167
return {
168+
slugifyTitle: (str: string) => {
169+
return (
170+
str
171+
// Remove control characters
172+
.replace(rControl, '')
173+
// Replace special characters
174+
.replace(rSpecial, '-')
175+
// Remove continuos separators
176+
.replace(/\-{2,}/g, '-')
177+
// Remove prefixing and trailing separtors
178+
.replace(/^\-+|\-+$/g, '')
179+
// ensure it doesn't start with a number (#121)
180+
.replace(/^(\d)/, '_$1')
181+
);
182+
},
165183
themeMode,
166184
visible,
167185
isMobile: globalConfig.isMobile,

0 commit comments

Comments
 (0)