Skip to content

Commit 3d21b00

Browse files
authored
fix: search scrolling (#8792)
* Push basic stuff * Push * Bump site-kit
1 parent 82cc483 commit 3d21b00

File tree

5 files changed

+58
-28
lines changed

5 files changed

+58
-28
lines changed

pnpm-lock.yaml

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sites/svelte.dev/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"@resvg/resvg-js": "^2.4.1",
3131
"@sveltejs/adapter-vercel": "^3.0.1",
3232
"@sveltejs/kit": "^1.20.4",
33-
"@sveltejs/site-kit": "6.0.0-next.14",
33+
"@sveltejs/site-kit": "6.0.0-next.16",
3434
"@sveltejs/vite-plugin-svelte": "^2.4.1",
3535
"@types/marked": "^5.0.0",
3636
"@types/node": "^20.3.1",

sites/svelte.dev/src/lib/server/docs/index.js

Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -99,29 +99,57 @@ export function get_docs_list(docs_data) {
9999
}));
100100
}
101101

102+
const titled = (str) =>
103+
removeMarkdown(
104+
escape(markedTransform(str, { paragraph: (txt) => txt }))
105+
.replace(/<\/?code>/g, '')
106+
.replace(/&#39;/g, "'")
107+
.replace(/&quot;/g, '"')
108+
.replace(/&lt;/g, '<')
109+
.replace(/&gt;/g, '>')
110+
.replace(/<(\/)?(em|b|strong|code)>/g, '')
111+
);
112+
102113
/** @param {string} markdown */
103114
function get_sections(markdown) {
104-
const headingRegex = /^##\s+(.*)$/gm;
105-
/** @type {import('./types').Section[]} */
106-
const secondLevelHeadings = [];
107-
let match;
108-
109-
const placeholders_rendered = replaceExportTypePlaceholders(markdown, modules);
110-
111-
while ((match = headingRegex.exec(placeholders_rendered)) !== null) {
112-
secondLevelHeadings.push({
113-
title: removeMarkdown(
114-
escape(markedTransform(match[1], { paragraph: (txt) => txt }))
115-
.replace(/<\/?code>/g, '')
116-
.replace(/&#39;/g, "'")
117-
.replace(/&quot;/g, '"')
118-
.replace(/&lt;/g, '<')
119-
.replace(/&gt;/g, '>')
120-
.replace(/<(\/)?(em|b|strong|code)>/g, '')
121-
),
122-
slug: normalizeSlugify(match[1])
123-
});
124-
}
115+
const lines = markdown.split('\n');
116+
const root = /** @type {import('./types').Section} */ ({
117+
title: 'Root',
118+
slug: 'root',
119+
sections: [],
120+
breadcrumbs: [''],
121+
text: ''
122+
});
123+
let currentNodes = [root];
124+
125+
lines.forEach((line) => {
126+
const match = line.match(/^(#{2,4})\s(.*)/);
127+
if (match) {
128+
const level = match[1].length - 2;
129+
const text = titled(match[2]);
130+
const slug = normalizeSlugify(text);
131+
132+
// Prepare new node
133+
/** @type {import('./types').Section} */
134+
const newNode = {
135+
title: text,
136+
slug,
137+
sections: [],
138+
breadcrumbs: [...currentNodes[level].breadcrumbs, text],
139+
text: ''
140+
};
141+
142+
// Add the new node to the tree
143+
currentNodes[level].sections.push(newNode);
144+
145+
// Prepare for potential children of the new node
146+
currentNodes = currentNodes.slice(0, level + 1);
147+
currentNodes.push(newNode);
148+
} else if (line.trim() !== '') {
149+
// Add non-heading line to the text of the current section
150+
currentNodes[currentNodes.length - 1].text += line + '\n';
151+
}
152+
});
125153

126-
return secondLevelHeadings;
154+
return root.sections;
127155
}

sites/svelte.dev/src/lib/server/docs/types.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ export interface Section {
55
slug: string;
66
// Currently, we are only going with 2 level headings, so this will be undefined. In future, we may want to support 3 levels, in which case this will be a list of sections
77
sections?: Section[];
8+
breadcrumbs: string[];
9+
text: string;
810
}
911

1012
export type Category = {

sites/svelte.dev/src/routes/content.json/content.server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export function content() {
7979
removeMarkdown(remove_TYPE(h2)),
8080
removeMarkdown(remove_TYPE(h3))
8181
],
82-
href: get_href([slug, normalizeSlugify(h2), normalizeSlugify(h3)]),
82+
href: get_href([slug, normalizeSlugify(h2) + '-' + normalizeSlugify(h3)]),
8383
content: plaintext(lines.join('\n').trim()),
8484
rank
8585
});

0 commit comments

Comments
 (0)