Skip to content

Commit b698085

Browse files
authored
fix slugs (#362)
* fix slugs - closes #345 * fix
1 parent 2feca8b commit b698085

File tree

5 files changed

+21
-15
lines changed

5 files changed

+21
-15
lines changed

apps/svelte.dev/content/docs/svelte/02-template-syntax/01-component-fundamentals.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ All three sections — script, styles and markup — are optional.
2121
</style>
2222
```
2323

24-
## &lt;script&gt;
24+
## `<script>`
2525

2626
A `<script>` block contains JavaScript (or TypeScript, when adding the `lang="ts"` attribute) that runs when a component instance is created. Variables declared (or imported) at the top level are 'visible' from the component's markup.
2727

@@ -161,7 +161,7 @@ If you'd like to react to changes to a prop, use the `$derived` or `$effect` run
161161

162162
For more information on reactivity, read the documentation around runes.
163163

164-
## &lt;script module&gt;
164+
## `<script module>`
165165

166166
A `<script>` tag with a `module` attribute runs once when the module first evaluates, rather than for each component instance. Values declared in this block are accessible from a regular `<script>` (and the component markup) but not vice versa.
167167

@@ -186,7 +186,7 @@ You cannot `export default`, since the default export is the component itself.
186186
</script>
187187
```
188188

189-
## &lt;style&gt;
189+
## `<style>`
190190

191191
CSS inside a `<style>` block will be scoped to that component.
192192

apps/svelte.dev/src/routes/docs/[...path]/OnThisPage.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@
5050

5151
{#each document.sections as section}
5252
<li>
53-
<a href="#{section.slug}" class:active={current === section.slug}>{section.title}</a>
53+
<a href="#{section.slug}" class:active={current === section.slug}>{@html section.title}</a
54+
>
5455
</li>
5556
{/each}
5657
</ul>

packages/site-kit/src/lib/markdown/renderer.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -274,16 +274,15 @@ export async function render_content_markdown(
274274

275275
return smart_quotes(token.text, true);
276276
},
277-
heading({ tokens, depth, raw }) {
277+
heading({ tokens, depth }) {
278278
const text = this.parser!.parseInline(tokens);
279+
const html = text.replace(/<\/?code>/g, '');
279280

280-
headings[depth - 1] = slugify(raw);
281+
headings[depth - 1] = slugify(text);
281282
headings.length = depth;
282283
const slug = headings.filter(Boolean).join('-');
283-
return `<h${depth} id="${slug}"><span>${text.replace(
284-
/<\/?code>/g,
285-
''
286-
)}</span><a href="#${slug}" class="permalink"><span class="visually-hidden">permalink</span></a></h${depth}>`;
284+
285+
return `<h${depth} id="${slug}"><span>${html}</span><a href="#${slug}" class="permalink" aria-label="permalink"></a></h${depth}>`;
287286
},
288287
code({ text }) {
289288
return snippets.get(text);

packages/site-kit/src/lib/markdown/utils.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@ export function clean(markdown: string) {
3232

3333
export const slugify = (str: string) => {
3434
return clean(str)
35-
.replace(/&.+;/g, '')
36-
.replace(/[^a-zA-Z0-9-$(.):]/g, '-')
35+
.replace(/&.+?;/g, '')
36+
.replace(/<\/?.+?>/g, '')
37+
.replace(/\.\.\./g, '')
38+
.replace(/[^a-zA-Z0-9-$(.):']/g, '-')
3739
.replace(/-{2,}/g, '-')
3840
.replace(/^-/, '')
39-
.replace(/-$/, '')
40-
.replace(/(<([^>]+)>)/gi, '');
41+
.replace(/-$/, '');
4142
};
4243

4344
export function smart_quotes(str: string, html: boolean = false) {

packages/site-kit/src/lib/server/content/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ export async function create_index(
3232
);
3333

3434
const sections = Array.from(body.matchAll(/^##\s+(.*)$/gm)).map((match) => {
35-
const title = match[1].replace(/`/g, '').replace(/&lt;/g, '<').replace(/&gt;/g, '>');
35+
const title = match[1]
36+
// replace < and > inside code spans
37+
.replace(/`(.+?)`/, (_, contents) => contents.replace(/</g, '&lt;').replace(/>/g, '&gt;'))
38+
// turn e.g. `class:_name_` into `class:<em>name</em>`
39+
.replace(/_(.+)_/g, (_, contents) => `<em>${contents}</em>`);
40+
3641
const slug = slugify(title);
3742

3843
return { slug, title };

0 commit comments

Comments
 (0)