|
1 | 1 | import { type Node, parse } from "@progfay/scrapbox-parser";
|
2 |
| -import type { BaseLine } from "@cosense/types/userscript"; |
3 | 2 | import { toTitleLc } from "../title.ts";
|
4 | 3 | import { parseYoutube } from "../parser/youtube.ts";
|
5 | 4 |
|
6 | 5 | /** Extract metadata from Scrapbox page text
|
7 | 6 | *
|
8 |
| - * This function parses a Scrapbox page and extracts various types of metadata: |
| 7 | + * ```ts |
| 8 | + * import { assertEquals } from "@std/assert/equals"; |
| 9 | + * |
| 10 | + * const text = `test page |
| 11 | + * [normal]link |
| 12 | + * but \`this [link]\` is not a link |
| 13 | + * |
| 14 | + * code:code |
| 15 | + * Links [link] and images [https://scrapbox.io/files/65f29c0c9045b5002522c8bb.svg] in code blocks should be ignored |
| 16 | + * |
| 17 | + * ? Need help with setup!! |
| 18 | + * |
| 19 | + * table:infobox |
| 20 | + * Name [scrapbox.icon] |
| 21 | + * Address Add [link2] here |
| 22 | + * Phone Adding # won't create a link |
| 23 | + * Strengths List about 3 items |
| 24 | + * |
| 25 | + * \#hashtag is recommended |
| 26 | + * [/forum-en] links should be excluded |
| 27 | + * [/help-en/] too |
| 28 | + * [/icons/example.icon][takker.icon] |
| 29 | + * [/help-en/external-link] |
| 30 | + * |
| 31 | + * Prepare thumbnail |
| 32 | + * [https://scrapbox.io/files/65f29c24974fd8002333b160.svg] |
| 33 | + * |
| 34 | + * [https://scrapbox.io/files/65e7f4413bc95600258481fb.svg https://scrapbox.io/files/65e7f82e03949c0024a367d0.svg]`; |
| 35 | + * |
| 36 | + * assertEquals(getPageMetadataFromLines(text), [ |
| 37 | + * "test page", |
| 38 | + * [ |
| 39 | + * "normal", |
| 40 | + * "link2", |
| 41 | + * "hashtag", |
| 42 | + * ], |
| 43 | + * [ |
| 44 | + * "/help-en/external-link", |
| 45 | + * ], |
| 46 | + * [ |
| 47 | + * "scrapbox", |
| 48 | + * "takker", |
| 49 | + * ], |
| 50 | + * "https://scrapbox.io/files/65f29c24974fd8002333b160.svg", |
| 51 | + * [ |
| 52 | + * "[normal]link", |
| 53 | + * "but `this [link]` is not a link", |
| 54 | + * "`Links [link] and images [https://scrapbox.io/files/65f29c0c9045b5002522c8bb.svg] in code blocks should be ignored`", |
| 55 | + * "`? Need help with setup!!`", |
| 56 | + * "#hashtag is recommended", |
| 57 | + * ], |
| 58 | + * [ |
| 59 | + * "65f29c24974fd8002333b160", |
| 60 | + * "65e7f82e03949c0024a367d0", |
| 61 | + * "65e7f4413bc95600258481fb", |
| 62 | + * ], |
| 63 | + * [ |
| 64 | + * "Need help with setup!!", |
| 65 | + * ], |
| 66 | + * [ |
| 67 | + * "Name\t[scrapbox.icon]", |
| 68 | + * "Address\tAdd [link2] here", |
| 69 | + * "Phone\tAdding # won't create a link", |
| 70 | + * "Strengths\tList about 3 items", |
| 71 | + * ], |
| 72 | + * 25, |
| 73 | + * 659, |
| 74 | + * ]); |
| 75 | + * ``` |
| 76 | + * |
| 77 | + * @param text - Raw text content of a Scrapbox page |
| 78 | + * @returns A tuple containing `[links, projectLinks, icons, image, files, helpfeels, infoboxDefinition]` |
9 | 79 | * - links: Regular page links and hashtags
|
10 | 80 | * - projectLinks: Links to pages in other projects
|
11 | 81 | * - icons: User icons and decorative icons
|
12 |
| - * - image: First image or YouTube thumbnail for page preview |
| 82 | + * - image: First image or YouTube thumbnail for page preview, which can be null if no suitable preview image is found |
13 | 83 | * - files: Attached file IDs
|
14 | 84 | * - helpfeels: Questions or help requests (lines starting with "?")
|
15 | 85 | * - infoboxDefinition: Structured data from infobox tables
|
16 |
| - * |
17 |
| - * @param text - Raw text content of a Scrapbox page |
18 |
| - * @returns A tuple containing [links, projectLinks, icons, image, files, helpfeels, infoboxDefinition] |
19 |
| - * where image can be null if no suitable preview image is found |
20 | 86 | */
|
21 | 87 | export const getPageMetadataFromLines = (
|
22 | 88 | text: string,
|
@@ -212,14 +278,3 @@ const makeInlineCodeForDescription = (text: string): `\`${string}\`` =>
|
212 | 278 | `\`${text.trim().replaceAll("`", "\\`").slice(0, 198)}\``;
|
213 | 279 |
|
214 | 280 | const cutId = (link: string): string => link.replace(/#[a-f\d]{24,32}$/, "");
|
215 |
| - |
216 |
| -/** Extract Helpfeel entries from text |
217 |
| - * |
218 |
| - * Helpfeel is a Scrapbox notation for questions and help requests. |
219 |
| - * Lines starting with "?" are considered Helpfeel entries and are |
220 |
| - * used to collect questions and support requests within a project. |
221 |
| - */ |
222 |
| -export const getHelpfeels = (lines: Pick<BaseLine, "text">[]): string[] => |
223 |
| - lines.flatMap(({ text }) => |
224 |
| - /^\s*\? .*$/.test(text) ? [text.trimStart().slice(2)] : [] |
225 |
| - ); |
|
0 commit comments