Skip to content

Commit 7fec868

Browse files
authored
Merge pull request #232 from takker99:doc-test
refactor: Move `getHelpfeels` function to a separate file
2 parents 658cb35 + f8966dd commit 7fec868

File tree

4 files changed

+105
-113
lines changed

4 files changed

+105
-113
lines changed

websocket/getHelpfeels.ts

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import type { BaseLine } from "@cosense/types/userscript";
2+
3+
/** Extract Helpfeel entries from text
4+
*
5+
* Helpfeel is a Scrapbox notation for questions and help requests.
6+
* Lines starting with "?" are considered Helpfeel entries and are
7+
* used to collect questions and support requests within a project.
8+
*
9+
* ```ts
10+
* import { assertEquals } from "@std/assert/equals";
11+
*
12+
* const text = `test page
13+
* [normal]link
14+
* but \`this [link]\` is not a link
15+
*
16+
* code:code
17+
* Links [link] and images [https://scrapbox.io/files/65f29c0c9045b5002522c8bb.svg] in code blocks should be ignored
18+
*
19+
* ? Need help with setup!!
20+
* `;
21+
*
22+
* assertEquals(getHelpfeels(text.split("\n").map((text) => ({ text }))), [
23+
* "Need help with setup!!",
24+
* ]);
25+
* ```
26+
*/
27+
export const getHelpfeels = (lines: Pick<BaseLine, "text">[]): string[] =>
28+
lines.flatMap(({ text }) =>
29+
/^\s*\? .*$/.test(text) ? [text.trimStart().slice(2)] : []
30+
);

websocket/getPageMetadataFromLines.test.ts

-91
This file was deleted.

websocket/getPageMetadataFromLines.ts

+73-18
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,88 @@
11
import { type Node, parse } from "@progfay/scrapbox-parser";
2-
import type { BaseLine } from "@cosense/types/userscript";
32
import { toTitleLc } from "../title.ts";
43
import { parseYoutube } from "../parser/youtube.ts";
54

65
/** Extract metadata from Scrapbox page text
76
*
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]`
979
* - links: Regular page links and hashtags
1080
* - projectLinks: Links to pages in other projects
1181
* - 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
1383
* - files: Attached file IDs
1484
* - helpfeels: Questions or help requests (lines starting with "?")
1585
* - 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
2086
*/
2187
export const getPageMetadataFromLines = (
2288
text: string,
@@ -212,14 +278,3 @@ const makeInlineCodeForDescription = (text: string): `\`${string}\`` =>
212278
`\`${text.trim().replaceAll("`", "\\`").slice(0, 198)}\``;
213279

214280
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-
);

websocket/makeChanges.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import { diffToChanges } from "./diffToChanges.ts";
22
import type { Page } from "@cosense/types/rest";
33
import type { ChangeToPush } from "@cosense/types/websocket";
4-
import {
5-
getHelpfeels,
6-
getPageMetadataFromLines,
7-
} from "./getPageMetadataFromLines.ts";
4+
import { getPageMetadataFromLines } from "./getPageMetadataFromLines.ts";
85
import { isSameArray } from "./isSameArray.ts";
96
import { isString } from "@core/unknownutil/is/string";
7+
import { getHelpfeels } from "./getHelpfeels.ts";
108

119
export function* makeChanges(
1210
before: Page,

0 commit comments

Comments
 (0)