Skip to content

Commit 8e4726f

Browse files
authored
fix: index lists and tables correctly for search (#1665)
1 parent 1eb3ae4 commit 8e4726f

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

apps/svelte.dev/src/routes/content.json/+server.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { Tokens } from 'marked';
12
import { index, docs as _docs, examples } from '$lib/server/content';
23
import { json } from '@sveltejs/kit';
34
import { transform, slugify, clean } from '@sveltejs/site-kit/markdown';
@@ -97,7 +98,14 @@ async function content() {
9798
}
9899

99100
async function plaintext(markdown: string) {
100-
const block = ({ text }: any) => `${text}\n`;
101+
const block = (token: Tokens.Blockquote | Tokens.ListItem | Tokens.TableRow) => {
102+
if ('text' in token) {
103+
return token.text;
104+
}
105+
// this should never happen, but abort loudly if it does
106+
console.error(token);
107+
throw new Error('Unexpected token while generating /content.json (see above)');
108+
};
101109

102110
const inline = ({ text }: any) => text;
103111

@@ -115,13 +123,21 @@ async function plaintext(markdown: string) {
115123
html: () => '\n',
116124
heading: ({ text }) => `${text}\n`,
117125
hr: () => '',
118-
list: block,
126+
list({ items }) {
127+
return items.map(this.listitem!).join('\n');
128+
},
119129
listitem: block,
120-
checkbox: block,
130+
checkbox: () => '',
121131
paragraph({ tokens }) {
122132
return this.parser!.parseInline(tokens);
123133
},
124-
table: block,
134+
table({ header, rows }) {
135+
const tHeader = this.tablerow!({ text: header.map(this.tablecell!).join() });
136+
const tBody = rows
137+
.map((row) => this.tablerow!({ text: row.map(this.tablecell!).join() }))
138+
.join('\n');
139+
return `${tHeader}\n${tBody}`;
140+
},
125141
tablerow: block,
126142
tablecell: ({ text }) => {
127143
return text + ' ';

0 commit comments

Comments
 (0)