|
1 |
| -import { extract_frontmatter, slugify, smart_quotes } from '../../markdown/utils'; |
| 1 | +import { extract_frontmatter, is_in_code_block, slugify, smart_quotes } from '../../markdown/utils'; |
2 | 2 | import type { Document } from '../../types';
|
3 | 3 |
|
4 | 4 | export async function create_index(
|
@@ -31,17 +31,24 @@ export async function create_index(
|
31 | 31 | '<code>$1</code>'
|
32 | 32 | );
|
33 | 33 |
|
34 |
| - const sections = Array.from(body.matchAll(/^##\s+(.*)$/gm)).map((match) => { |
35 |
| - const title = smart_quotes(match[1]) |
36 |
| - // replace < and > inside code spans |
37 |
| - .replace(/`(.+?)`/g, (_, contents) => contents.replace(/</g, '<').replace(/>/g, '>')) |
38 |
| - // turn e.g. `class:_name_` into `class:<em>name</em>` |
39 |
| - .replace(/_(.+)_/g, (_, contents) => `<em>${contents}</em>`); |
40 |
| - |
41 |
| - const slug = slugify(title); |
42 |
| - |
43 |
| - return { slug, title }; |
44 |
| - }); |
| 34 | + const sections = Array.from(body.matchAll(/^##\s+(.*)$/gm)).reduce( |
| 35 | + (arr, match) => { |
| 36 | + if (is_in_code_block(body, match.index || 0)) return arr; |
| 37 | + const title = smart_quotes(match[1]) |
| 38 | + // replace < and > inside code spans |
| 39 | + .replace(/`(.+?)`/g, (_, contents) => |
| 40 | + contents.replace(/</g, '<').replace(/>/g, '>') |
| 41 | + ) |
| 42 | + // turn e.g. `class:_name_` into `class:<em>name</em>` |
| 43 | + .replace(/_(.+)_/g, (_, contents) => `<em>${contents}</em>`); |
| 44 | + |
| 45 | + const slug = slugify(title); |
| 46 | + |
| 47 | + arr.push({ slug, title }); |
| 48 | + return arr; |
| 49 | + }, |
| 50 | + [] as Array<{ slug: string; title: string }> |
| 51 | + ); |
45 | 52 |
|
46 | 53 | content[slug] = {
|
47 | 54 | slug,
|
|
0 commit comments