@cosense-toolbox/parser 0.1.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +794 -0
  3. package/dist/ast-CIKXwSl5.mjs +41 -0
  4. package/dist/ast-CIKXwSl5.mjs.map +1 -0
  5. package/dist/ast-uYWtkHwT.cjs +52 -0
  6. package/dist/ast-uYWtkHwT.cjs.map +1 -0
  7. package/dist/compile.cjs +265 -0
  8. package/dist/compile.cjs.map +1 -0
  9. package/dist/compile.d.cts +135 -0
  10. package/dist/compile.d.cts.map +1 -0
  11. package/dist/compile.d.mts +135 -0
  12. package/dist/compile.d.mts.map +1 -0
  13. package/dist/compile.mjs +256 -0
  14. package/dist/compile.mjs.map +1 -0
  15. package/dist/create-compiler-CvlndKOA.d.cts +23 -0
  16. package/dist/create-compiler-CvlndKOA.d.cts.map +1 -0
  17. package/dist/create-compiler-jdA408ny.d.mts +23 -0
  18. package/dist/create-compiler-jdA408ny.d.mts.map +1 -0
  19. package/dist/image-url-YcTed8tg.cjs +68 -0
  20. package/dist/image-url-YcTed8tg.cjs.map +1 -0
  21. package/dist/image-url-eQ6X-oN0.mjs +51 -0
  22. package/dist/image-url-eQ6X-oN0.mjs.map +1 -0
  23. package/dist/index.cjs +716 -0
  24. package/dist/index.cjs.map +1 -0
  25. package/dist/index.d.cts +77 -0
  26. package/dist/index.d.cts.map +1 -0
  27. package/dist/index.d.mts +77 -0
  28. package/dist/index.d.mts.map +1 -0
  29. package/dist/index.mjs +709 -0
  30. package/dist/index.mjs.map +1 -0
  31. package/dist/plugin.cjs +0 -0
  32. package/dist/plugin.d.cts +4 -0
  33. package/dist/plugin.d.mts +4 -0
  34. package/dist/plugin.mjs +1 -0
  35. package/dist/schema.cjs +171 -0
  36. package/dist/schema.cjs.map +1 -0
  37. package/dist/schema.d.cts +34 -0
  38. package/dist/schema.d.cts.map +1 -0
  39. package/dist/schema.d.mts +34 -0
  40. package/dist/schema.d.mts.map +1 -0
  41. package/dist/schema.mjs +148 -0
  42. package/dist/schema.mjs.map +1 -0
  43. package/dist/types-BXlnUFr0.d.mts +63 -0
  44. package/dist/types-BXlnUFr0.d.mts.map +1 -0
  45. package/dist/types-Bo_BrmvK.d.cts +230 -0
  46. package/dist/types-Bo_BrmvK.d.cts.map +1 -0
  47. package/dist/types-Bo_BrmvK.d.mts +230 -0
  48. package/dist/types-Bo_BrmvK.d.mts.map +1 -0
  49. package/dist/types-DVwlUtla.d.cts +63 -0
  50. package/dist/types-DVwlUtla.d.cts.map +1 -0
  51. package/dist/utils.cjs +80 -0
  52. package/dist/utils.cjs.map +1 -0
  53. package/dist/utils.d.cts +46 -0
  54. package/dist/utils.d.cts.map +1 -0
  55. package/dist/utils.d.mts +46 -0
  56. package/dist/utils.d.mts.map +1 -0
  57. package/dist/utils.mjs +72 -0
  58. package/dist/utils.mjs.map +1 -0
  59. package/package.json +96 -0
package/dist/utils.cjs ADDED
@@ -0,0 +1,80 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_ast = require("./ast-uYWtkHwT.cjs");
3
+ //#region src/utils/visit.ts
4
+ /**
5
+ * visit.ts — unist-util-visit 風の走査。外部依存を増やさないための自前実装。
6
+ */
7
+ function visit(tree, typesOrVisitor, maybeVisitor) {
8
+ const visitor = maybeVisitor ?? typesOrVisitor;
9
+ const types = maybeVisitor === void 0 ? void 0 : new Set(typeof typesOrVisitor === "string" ? [typesOrVisitor] : typesOrVisitor);
10
+ const walk = (node, ancestors) => {
11
+ if (types === void 0 || types.has(node.type)) {
12
+ const result = visitor(node, ancestors);
13
+ if (result === "exit") return "exit";
14
+ if (result === "skip") return void 0;
15
+ }
16
+ const nextAncestors = [...ancestors, node];
17
+ for (const child of require_ast.childrenOf(node)) if (walk(child, nextAncestors) === "exit") return "exit";
18
+ };
19
+ walk(tree, []);
20
+ }
21
+ /** その型の最初のノードを返す。無ければ null。 */
22
+ function find(tree, type) {
23
+ let found = null;
24
+ visit(tree, type, (node) => {
25
+ found = node;
26
+ return "exit";
27
+ });
28
+ return found;
29
+ }
30
+ /** その型のノードを出現順にすべて集める。 */
31
+ function collect(tree, type) {
32
+ const out = [];
33
+ visit(tree, type, (node) => {
34
+ out.push(node);
35
+ });
36
+ return out;
37
+ }
38
+ //#endregion
39
+ //#region src/utils/links.ts
40
+ /**
41
+ * 同じプロジェクト内のページを指すリンクを、出現順・重複なしで集める。
42
+ * `[title]` と `#tag` は同じページを指すので同じ 1 件として扱う。
43
+ */
44
+ const collectLinks = (tree, options) => {
45
+ const out = [];
46
+ const seen = /* @__PURE__ */ new Set();
47
+ const add = (value) => {
48
+ if (value === "" || seen.has(value)) return;
49
+ seen.add(value);
50
+ out.push(value);
51
+ };
52
+ visit(tree, (node) => {
53
+ if (node.type === "internalLink") add(node.target);
54
+ else if (node.type === "hashtag") add(node.value);
55
+ else if (node.type === "projectLink" && options?.includeProjectLinks) add(node.target);
56
+ });
57
+ return out;
58
+ };
59
+ /** ページが参照している外部プロジェクトのリンクを、出現順に集める。 */
60
+ const collectProjectLinks = (tree) => collect(tree, "projectLink");
61
+ /** 最初に現れる画像ノード。無ければ null。 */
62
+ const firstImage = (tree) => {
63
+ let found = null;
64
+ visit(tree, "image", (node) => {
65
+ found = node;
66
+ return "exit";
67
+ });
68
+ return found;
69
+ };
70
+ //#endregion
71
+ exports.childrenOf = require_ast.childrenOf;
72
+ exports.collect = collect;
73
+ exports.collectLinks = collectLinks;
74
+ exports.collectProjectLinks = collectProjectLinks;
75
+ exports.find = find;
76
+ exports.firstImage = firstImage;
77
+ exports.rawTextOf = require_ast.rawTextOf;
78
+ exports.visit = visit;
79
+
80
+ //# sourceMappingURL=utils.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.cjs","names":["childrenOf"],"sources":["../src/utils/visit.ts","../src/utils/links.ts"],"sourcesContent":["/**\n * visit.ts — unist-util-visit 風の走査。外部依存を増やさないための自前実装。\n */\nimport { childrenOf } from '../ast'\nimport type { AnyNode, AnyNodeType, NodeOfType } from '../types'\n\n/**\n * visitor の戻り値。\n * - 何も返さない — そのまま子へ進む\n * - `'skip'` — このノードの子を辿らない\n * - `'exit'` — 走査全体を打ち切る\n */\n// biome-ignore lint/suspicious/noConfusingVoidType: undefined だと値を返さない visitor を渡せない\nexport type VisitResult = void | 'skip' | 'exit'\n\nexport type Visitor<T extends AnyNode> = (node: T, ancestors: readonly AnyNode[]) => VisitResult\n\n/** 木を深さ優先で辿る。`types` を渡すとその型のノードだけが visitor に届く。 */\nexport function visit(tree: AnyNode, visitor: Visitor<AnyNode>): void\nexport function visit<K extends AnyNodeType>(\n tree: AnyNode,\n types: K | readonly K[],\n visitor: Visitor<NodeOfType<K>>,\n): void\nexport function visit(\n tree: AnyNode,\n typesOrVisitor: AnyNodeType | readonly AnyNodeType[] | Visitor<AnyNode>,\n maybeVisitor?: Visitor<AnyNode>,\n): void {\n const visitor = maybeVisitor ?? (typesOrVisitor as Visitor<AnyNode>)\n const types =\n maybeVisitor === undefined\n ? undefined\n : new Set(\n typeof typesOrVisitor === 'string'\n ? [typesOrVisitor]\n : (typesOrVisitor as readonly AnyNodeType[]),\n )\n\n const walk = (node: AnyNode, ancestors: readonly AnyNode[]): VisitResult => {\n if (types === undefined || types.has(node.type)) {\n const result = visitor(node, ancestors)\n if (result === 'exit') return 'exit'\n if (result === 'skip') return undefined\n }\n const nextAncestors = [...ancestors, node]\n for (const child of childrenOf(node)) {\n if (walk(child, nextAncestors) === 'exit') return 'exit'\n }\n return undefined\n }\n\n walk(tree, [])\n}\n\n/** その型の最初のノードを返す。無ければ null。 */\nexport function find<K extends AnyNodeType>(tree: AnyNode, type: K): NodeOfType<K> | null {\n let found: NodeOfType<K> | null = null\n visit(tree, type, (node) => {\n found = node\n return 'exit'\n })\n return found\n}\n\n/** その型のノードを出現順にすべて集める。 */\nexport function collect<K extends AnyNodeType>(tree: AnyNode, type: K): NodeOfType<K>[] {\n const out: NodeOfType<K>[] = []\n visit(tree, type, (node) => {\n out.push(node)\n })\n return out\n}\n","/**\n * links.ts — AST からリンクと画像を抜き出す。\n */\nimport type { AnyNode, ImageNode, ProjectLink } from '../types'\nimport { collect, visit } from './visit'\n\nexport interface CollectLinksOptions {\n /** `[/project/title]` も含めるか (既定: false)。同一プロジェクト内の被リンクだけが要るなら false */\n readonly includeProjectLinks?: boolean\n}\n\n/**\n * 同じプロジェクト内のページを指すリンクを、出現順・重複なしで集める。\n * `[title]` と `#tag` は同じページを指すので同じ 1 件として扱う。\n */\nexport const collectLinks = (tree: AnyNode, options?: CollectLinksOptions): string[] => {\n const out: string[] = []\n const seen = new Set<string>()\n const add = (value: string) => {\n if (value === '' || seen.has(value)) return\n seen.add(value)\n out.push(value)\n }\n\n visit(tree, (node) => {\n if (node.type === 'internalLink') add(node.target)\n else if (node.type === 'hashtag') add(node.value)\n else if (node.type === 'projectLink' && options?.includeProjectLinks) add(node.target)\n })\n\n return out\n}\n\n/** ページが参照している外部プロジェクトのリンクを、出現順に集める。 */\nexport const collectProjectLinks = (tree: AnyNode): ProjectLink[] => collect(tree, 'projectLink')\n\n/** 最初に現れる画像ノード。無ければ null。 */\nexport const firstImage = (tree: AnyNode): ImageNode | null => {\n let found: ImageNode | null = null\n visit(tree, 'image', (node) => {\n found = node\n return 'exit'\n })\n return found\n}\n"],"mappings":";;;;;;AAwBA,SAAgB,MACd,MACA,gBACA,cACM;CACN,MAAM,UAAU,gBAAiB;CACjC,MAAM,QACJ,iBAAiB,KAAA,IACb,KAAA,IACA,IAAI,IACF,OAAO,mBAAmB,WACtB,CAAC,cAAc,IACd,cACP;CAEN,MAAM,QAAQ,MAAe,cAA+C;EAC1E,IAAI,UAAU,KAAA,KAAa,MAAM,IAAI,KAAK,IAAI,GAAG;GAC/C,MAAM,SAAS,QAAQ,MAAM,SAAS;GACtC,IAAI,WAAW,QAAQ,OAAO;GAC9B,IAAI,WAAW,QAAQ,OAAO,KAAA;EAChC;EACA,MAAM,gBAAgB,CAAC,GAAG,WAAW,IAAI;EACzC,KAAK,MAAM,SAASA,YAAAA,WAAW,IAAI,GACjC,IAAI,KAAK,OAAO,aAAa,MAAM,QAAQ,OAAO;CAGtD;CAEA,KAAK,MAAM,CAAC,CAAC;AACf;;AAGA,SAAgB,KAA4B,MAAe,MAA+B;CACxF,IAAI,QAA8B;CAClC,MAAM,MAAM,OAAO,SAAS;EAC1B,QAAQ;EACR,OAAO;CACT,CAAC;CACD,OAAO;AACT;;AAGA,SAAgB,QAA+B,MAAe,MAA0B;CACtF,MAAM,MAAuB,CAAC;CAC9B,MAAM,MAAM,OAAO,SAAS;EAC1B,IAAI,KAAK,IAAI;CACf,CAAC;CACD,OAAO;AACT;;;;;;;ACzDA,MAAa,gBAAgB,MAAe,YAA4C;CACtF,MAAM,MAAgB,CAAC;CACvB,MAAM,uBAAO,IAAI,IAAY;CAC7B,MAAM,OAAO,UAAkB;EAC7B,IAAI,UAAU,MAAM,KAAK,IAAI,KAAK,GAAG;EACrC,KAAK,IAAI,KAAK;EACd,IAAI,KAAK,KAAK;CAChB;CAEA,MAAM,OAAO,SAAS;EACpB,IAAI,KAAK,SAAS,gBAAgB,IAAI,KAAK,MAAM;OAC5C,IAAI,KAAK,SAAS,WAAW,IAAI,KAAK,KAAK;OAC3C,IAAI,KAAK,SAAS,iBAAiB,SAAS,qBAAqB,IAAI,KAAK,MAAM;CACvF,CAAC;CAED,OAAO;AACT;;AAGA,MAAa,uBAAuB,SAAiC,QAAQ,MAAM,aAAa;;AAGhG,MAAa,cAAc,SAAoC;CAC7D,IAAI,QAA0B;CAC9B,MAAM,MAAM,UAAU,SAAS;EAC7B,QAAQ;EACR,OAAO;CACT,CAAC;CACD,OAAO;AACT"}
@@ -0,0 +1,46 @@
1
+ import { C as NodeOfType, D as ProjectLink, m as ImageNode, r as AnyNodeType, t as AnyNode } from "./types-Bo_BrmvK.cjs";
2
+ //#region src/ast.d.ts
3
+ /**
4
+ * ノードの子を返す。子を持たないノードでは空配列。
5
+ *
6
+ * どのフィールドが子にあたるかはノード型ごとに違う (`children` / `lines` / `rows` / `cells`)。
7
+ * その対応を知っているのはここだけで、AST を辿るコードはすべてこれを通る。
8
+ */
9
+ declare const childrenOf: (node: AnyNode) => readonly AnyNode[];
10
+ /** そのノードがソース上で占めていた生のテキスト。位置情報から復元する。 */
11
+ declare const rawTextOf: (source: string, node: AnyNode) => string;
12
+ //#endregion
13
+ //#region src/utils/links.d.ts
14
+ interface CollectLinksOptions {
15
+ /** `[/project/title]` も含めるか (既定: false)。同一プロジェクト内の被リンクだけが要るなら false */
16
+ readonly includeProjectLinks?: boolean;
17
+ }
18
+ /**
19
+ * 同じプロジェクト内のページを指すリンクを、出現順・重複なしで集める。
20
+ * `[title]` と `#tag` は同じページを指すので同じ 1 件として扱う。
21
+ */
22
+ declare const collectLinks: (tree: AnyNode, options?: CollectLinksOptions) => string[];
23
+ /** ページが参照している外部プロジェクトのリンクを、出現順に集める。 */
24
+ declare const collectProjectLinks: (tree: AnyNode) => ProjectLink[];
25
+ /** 最初に現れる画像ノード。無ければ null。 */
26
+ declare const firstImage: (tree: AnyNode) => ImageNode | null;
27
+ //#endregion
28
+ //#region src/utils/visit.d.ts
29
+ /**
30
+ * visitor の戻り値。
31
+ * - 何も返さない — そのまま子へ進む
32
+ * - `'skip'` — このノードの子を辿らない
33
+ * - `'exit'` — 走査全体を打ち切る
34
+ */
35
+ type VisitResult = void | 'skip' | 'exit';
36
+ type Visitor<T extends AnyNode> = (node: T, ancestors: readonly AnyNode[]) => VisitResult;
37
+ /** 木を深さ優先で辿る。`types` を渡すとその型のノードだけが visitor に届く。 */
38
+ declare function visit(tree: AnyNode, visitor: Visitor<AnyNode>): void;
39
+ declare function visit<K extends AnyNodeType>(tree: AnyNode, types: K | readonly K[], visitor: Visitor<NodeOfType<K>>): void;
40
+ /** その型の最初のノードを返す。無ければ null。 */
41
+ declare function find<K extends AnyNodeType>(tree: AnyNode, type: K): NodeOfType<K> | null;
42
+ /** その型のノードを出現順にすべて集める。 */
43
+ declare function collect<K extends AnyNodeType>(tree: AnyNode, type: K): NodeOfType<K>[];
44
+ //#endregion
45
+ export { type CollectLinksOptions, type VisitResult, type Visitor, childrenOf, collect, collectLinks, collectProjectLinks, find, firstImage, rawTextOf, visit };
46
+ //# sourceMappingURL=utils.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.cts","names":[],"sources":["../src/ast.ts","../src/utils/links.ts","../src/utils/visit.ts"],"mappings":";;;;;;;;cAiBa,aAAc,MAAM,qBAAmB;;cAuCvC,YAAa,gBAAgB,MAAM;;;UClD/B;;WAEN;;;;;;cAOE,eAAgB,MAAM,SAAS,UAAU;;cAmBzC,sBAAuB,MAAM,YAAU;;cAGvC,aAAc,MAAM,YAAU;;;;;;;;;KCxB/B;KAEA,QAAQ,UAAU,YAAY,MAAM,GAAG,oBAAoB,cAAc;;iBAGrE,MAAM,MAAM,SAAS,SAAS,QAAQ;iBACtC,MAAM,UAAU,aAC9B,MAAM,SACN,OAAO,aAAa,KACpB,SAAS,QAAQ,WAAW;;iBAkCd,KAAK,UAAU,aAAa,MAAM,SAAS,MAAM,IAAI,WAAW;;iBAUhE,QAAQ,UAAU,aAAa,MAAM,SAAS,MAAM,IAAI,WAAW"}
@@ -0,0 +1,46 @@
1
+ import { C as NodeOfType, D as ProjectLink, m as ImageNode, r as AnyNodeType, t as AnyNode } from "./types-Bo_BrmvK.mjs";
2
+ //#region src/ast.d.ts
3
+ /**
4
+ * ノードの子を返す。子を持たないノードでは空配列。
5
+ *
6
+ * どのフィールドが子にあたるかはノード型ごとに違う (`children` / `lines` / `rows` / `cells`)。
7
+ * その対応を知っているのはここだけで、AST を辿るコードはすべてこれを通る。
8
+ */
9
+ declare const childrenOf: (node: AnyNode) => readonly AnyNode[];
10
+ /** そのノードがソース上で占めていた生のテキスト。位置情報から復元する。 */
11
+ declare const rawTextOf: (source: string, node: AnyNode) => string;
12
+ //#endregion
13
+ //#region src/utils/links.d.ts
14
+ interface CollectLinksOptions {
15
+ /** `[/project/title]` も含めるか (既定: false)。同一プロジェクト内の被リンクだけが要るなら false */
16
+ readonly includeProjectLinks?: boolean;
17
+ }
18
+ /**
19
+ * 同じプロジェクト内のページを指すリンクを、出現順・重複なしで集める。
20
+ * `[title]` と `#tag` は同じページを指すので同じ 1 件として扱う。
21
+ */
22
+ declare const collectLinks: (tree: AnyNode, options?: CollectLinksOptions) => string[];
23
+ /** ページが参照している外部プロジェクトのリンクを、出現順に集める。 */
24
+ declare const collectProjectLinks: (tree: AnyNode) => ProjectLink[];
25
+ /** 最初に現れる画像ノード。無ければ null。 */
26
+ declare const firstImage: (tree: AnyNode) => ImageNode | null;
27
+ //#endregion
28
+ //#region src/utils/visit.d.ts
29
+ /**
30
+ * visitor の戻り値。
31
+ * - 何も返さない — そのまま子へ進む
32
+ * - `'skip'` — このノードの子を辿らない
33
+ * - `'exit'` — 走査全体を打ち切る
34
+ */
35
+ type VisitResult = void | 'skip' | 'exit';
36
+ type Visitor<T extends AnyNode> = (node: T, ancestors: readonly AnyNode[]) => VisitResult;
37
+ /** 木を深さ優先で辿る。`types` を渡すとその型のノードだけが visitor に届く。 */
38
+ declare function visit(tree: AnyNode, visitor: Visitor<AnyNode>): void;
39
+ declare function visit<K extends AnyNodeType>(tree: AnyNode, types: K | readonly K[], visitor: Visitor<NodeOfType<K>>): void;
40
+ /** その型の最初のノードを返す。無ければ null。 */
41
+ declare function find<K extends AnyNodeType>(tree: AnyNode, type: K): NodeOfType<K> | null;
42
+ /** その型のノードを出現順にすべて集める。 */
43
+ declare function collect<K extends AnyNodeType>(tree: AnyNode, type: K): NodeOfType<K>[];
44
+ //#endregion
45
+ export { type CollectLinksOptions, type VisitResult, type Visitor, childrenOf, collect, collectLinks, collectProjectLinks, find, firstImage, rawTextOf, visit };
46
+ //# sourceMappingURL=utils.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.mts","names":[],"sources":["../src/ast.ts","../src/utils/links.ts","../src/utils/visit.ts"],"mappings":";;;;;;;;cAiBa,aAAc,MAAM,qBAAmB;;cAuCvC,YAAa,gBAAgB,MAAM;;;UClD/B;;WAEN;;;;;;cAOE,eAAgB,MAAM,SAAS,UAAU;;cAmBzC,sBAAuB,MAAM,YAAU;;cAGvC,aAAc,MAAM,YAAU;;;;;;;;;KCxB/B;KAEA,QAAQ,UAAU,YAAY,MAAM,GAAG,oBAAoB,cAAc;;iBAGrE,MAAM,MAAM,SAAS,SAAS,QAAQ;iBACtC,MAAM,UAAU,aAC9B,MAAM,SACN,OAAO,aAAa,KACpB,SAAS,QAAQ,WAAW;;iBAkCd,KAAK,UAAU,aAAa,MAAM,SAAS,MAAM,IAAI,WAAW;;iBAUhE,QAAQ,UAAU,aAAa,MAAM,SAAS,MAAM,IAAI,WAAW"}
package/dist/utils.mjs ADDED
@@ -0,0 +1,72 @@
1
+ import { n as rawTextOf, t as childrenOf } from "./ast-CIKXwSl5.mjs";
2
+ //#region src/utils/visit.ts
3
+ /**
4
+ * visit.ts — unist-util-visit 風の走査。外部依存を増やさないための自前実装。
5
+ */
6
+ function visit(tree, typesOrVisitor, maybeVisitor) {
7
+ const visitor = maybeVisitor ?? typesOrVisitor;
8
+ const types = maybeVisitor === void 0 ? void 0 : new Set(typeof typesOrVisitor === "string" ? [typesOrVisitor] : typesOrVisitor);
9
+ const walk = (node, ancestors) => {
10
+ if (types === void 0 || types.has(node.type)) {
11
+ const result = visitor(node, ancestors);
12
+ if (result === "exit") return "exit";
13
+ if (result === "skip") return void 0;
14
+ }
15
+ const nextAncestors = [...ancestors, node];
16
+ for (const child of childrenOf(node)) if (walk(child, nextAncestors) === "exit") return "exit";
17
+ };
18
+ walk(tree, []);
19
+ }
20
+ /** その型の最初のノードを返す。無ければ null。 */
21
+ function find(tree, type) {
22
+ let found = null;
23
+ visit(tree, type, (node) => {
24
+ found = node;
25
+ return "exit";
26
+ });
27
+ return found;
28
+ }
29
+ /** その型のノードを出現順にすべて集める。 */
30
+ function collect(tree, type) {
31
+ const out = [];
32
+ visit(tree, type, (node) => {
33
+ out.push(node);
34
+ });
35
+ return out;
36
+ }
37
+ //#endregion
38
+ //#region src/utils/links.ts
39
+ /**
40
+ * 同じプロジェクト内のページを指すリンクを、出現順・重複なしで集める。
41
+ * `[title]` と `#tag` は同じページを指すので同じ 1 件として扱う。
42
+ */
43
+ const collectLinks = (tree, options) => {
44
+ const out = [];
45
+ const seen = /* @__PURE__ */ new Set();
46
+ const add = (value) => {
47
+ if (value === "" || seen.has(value)) return;
48
+ seen.add(value);
49
+ out.push(value);
50
+ };
51
+ visit(tree, (node) => {
52
+ if (node.type === "internalLink") add(node.target);
53
+ else if (node.type === "hashtag") add(node.value);
54
+ else if (node.type === "projectLink" && options?.includeProjectLinks) add(node.target);
55
+ });
56
+ return out;
57
+ };
58
+ /** ページが参照している外部プロジェクトのリンクを、出現順に集める。 */
59
+ const collectProjectLinks = (tree) => collect(tree, "projectLink");
60
+ /** 最初に現れる画像ノード。無ければ null。 */
61
+ const firstImage = (tree) => {
62
+ let found = null;
63
+ visit(tree, "image", (node) => {
64
+ found = node;
65
+ return "exit";
66
+ });
67
+ return found;
68
+ };
69
+ //#endregion
70
+ export { childrenOf, collect, collectLinks, collectProjectLinks, find, firstImage, rawTextOf, visit };
71
+
72
+ //# sourceMappingURL=utils.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.mjs","names":[],"sources":["../src/utils/visit.ts","../src/utils/links.ts"],"sourcesContent":["/**\n * visit.ts — unist-util-visit 風の走査。外部依存を増やさないための自前実装。\n */\nimport { childrenOf } from '../ast'\nimport type { AnyNode, AnyNodeType, NodeOfType } from '../types'\n\n/**\n * visitor の戻り値。\n * - 何も返さない — そのまま子へ進む\n * - `'skip'` — このノードの子を辿らない\n * - `'exit'` — 走査全体を打ち切る\n */\n// biome-ignore lint/suspicious/noConfusingVoidType: undefined だと値を返さない visitor を渡せない\nexport type VisitResult = void | 'skip' | 'exit'\n\nexport type Visitor<T extends AnyNode> = (node: T, ancestors: readonly AnyNode[]) => VisitResult\n\n/** 木を深さ優先で辿る。`types` を渡すとその型のノードだけが visitor に届く。 */\nexport function visit(tree: AnyNode, visitor: Visitor<AnyNode>): void\nexport function visit<K extends AnyNodeType>(\n tree: AnyNode,\n types: K | readonly K[],\n visitor: Visitor<NodeOfType<K>>,\n): void\nexport function visit(\n tree: AnyNode,\n typesOrVisitor: AnyNodeType | readonly AnyNodeType[] | Visitor<AnyNode>,\n maybeVisitor?: Visitor<AnyNode>,\n): void {\n const visitor = maybeVisitor ?? (typesOrVisitor as Visitor<AnyNode>)\n const types =\n maybeVisitor === undefined\n ? undefined\n : new Set(\n typeof typesOrVisitor === 'string'\n ? [typesOrVisitor]\n : (typesOrVisitor as readonly AnyNodeType[]),\n )\n\n const walk = (node: AnyNode, ancestors: readonly AnyNode[]): VisitResult => {\n if (types === undefined || types.has(node.type)) {\n const result = visitor(node, ancestors)\n if (result === 'exit') return 'exit'\n if (result === 'skip') return undefined\n }\n const nextAncestors = [...ancestors, node]\n for (const child of childrenOf(node)) {\n if (walk(child, nextAncestors) === 'exit') return 'exit'\n }\n return undefined\n }\n\n walk(tree, [])\n}\n\n/** その型の最初のノードを返す。無ければ null。 */\nexport function find<K extends AnyNodeType>(tree: AnyNode, type: K): NodeOfType<K> | null {\n let found: NodeOfType<K> | null = null\n visit(tree, type, (node) => {\n found = node\n return 'exit'\n })\n return found\n}\n\n/** その型のノードを出現順にすべて集める。 */\nexport function collect<K extends AnyNodeType>(tree: AnyNode, type: K): NodeOfType<K>[] {\n const out: NodeOfType<K>[] = []\n visit(tree, type, (node) => {\n out.push(node)\n })\n return out\n}\n","/**\n * links.ts — AST からリンクと画像を抜き出す。\n */\nimport type { AnyNode, ImageNode, ProjectLink } from '../types'\nimport { collect, visit } from './visit'\n\nexport interface CollectLinksOptions {\n /** `[/project/title]` も含めるか (既定: false)。同一プロジェクト内の被リンクだけが要るなら false */\n readonly includeProjectLinks?: boolean\n}\n\n/**\n * 同じプロジェクト内のページを指すリンクを、出現順・重複なしで集める。\n * `[title]` と `#tag` は同じページを指すので同じ 1 件として扱う。\n */\nexport const collectLinks = (tree: AnyNode, options?: CollectLinksOptions): string[] => {\n const out: string[] = []\n const seen = new Set<string>()\n const add = (value: string) => {\n if (value === '' || seen.has(value)) return\n seen.add(value)\n out.push(value)\n }\n\n visit(tree, (node) => {\n if (node.type === 'internalLink') add(node.target)\n else if (node.type === 'hashtag') add(node.value)\n else if (node.type === 'projectLink' && options?.includeProjectLinks) add(node.target)\n })\n\n return out\n}\n\n/** ページが参照している外部プロジェクトのリンクを、出現順に集める。 */\nexport const collectProjectLinks = (tree: AnyNode): ProjectLink[] => collect(tree, 'projectLink')\n\n/** 最初に現れる画像ノード。無ければ null。 */\nexport const firstImage = (tree: AnyNode): ImageNode | null => {\n let found: ImageNode | null = null\n visit(tree, 'image', (node) => {\n found = node\n return 'exit'\n })\n return found\n}\n"],"mappings":";;;;;AAwBA,SAAgB,MACd,MACA,gBACA,cACM;CACN,MAAM,UAAU,gBAAiB;CACjC,MAAM,QACJ,iBAAiB,KAAA,IACb,KAAA,IACA,IAAI,IACF,OAAO,mBAAmB,WACtB,CAAC,cAAc,IACd,cACP;CAEN,MAAM,QAAQ,MAAe,cAA+C;EAC1E,IAAI,UAAU,KAAA,KAAa,MAAM,IAAI,KAAK,IAAI,GAAG;GAC/C,MAAM,SAAS,QAAQ,MAAM,SAAS;GACtC,IAAI,WAAW,QAAQ,OAAO;GAC9B,IAAI,WAAW,QAAQ,OAAO,KAAA;EAChC;EACA,MAAM,gBAAgB,CAAC,GAAG,WAAW,IAAI;EACzC,KAAK,MAAM,SAAS,WAAW,IAAI,GACjC,IAAI,KAAK,OAAO,aAAa,MAAM,QAAQ,OAAO;CAGtD;CAEA,KAAK,MAAM,CAAC,CAAC;AACf;;AAGA,SAAgB,KAA4B,MAAe,MAA+B;CACxF,IAAI,QAA8B;CAClC,MAAM,MAAM,OAAO,SAAS;EAC1B,QAAQ;EACR,OAAO;CACT,CAAC;CACD,OAAO;AACT;;AAGA,SAAgB,QAA+B,MAAe,MAA0B;CACtF,MAAM,MAAuB,CAAC;CAC9B,MAAM,MAAM,OAAO,SAAS;EAC1B,IAAI,KAAK,IAAI;CACf,CAAC;CACD,OAAO;AACT;;;;;;;ACzDA,MAAa,gBAAgB,MAAe,YAA4C;CACtF,MAAM,MAAgB,CAAC;CACvB,MAAM,uBAAO,IAAI,IAAY;CAC7B,MAAM,OAAO,UAAkB;EAC7B,IAAI,UAAU,MAAM,KAAK,IAAI,KAAK,GAAG;EACrC,KAAK,IAAI,KAAK;EACd,IAAI,KAAK,KAAK;CAChB;CAEA,MAAM,OAAO,SAAS;EACpB,IAAI,KAAK,SAAS,gBAAgB,IAAI,KAAK,MAAM;OAC5C,IAAI,KAAK,SAAS,WAAW,IAAI,KAAK,KAAK;OAC3C,IAAI,KAAK,SAAS,iBAAiB,SAAS,qBAAqB,IAAI,KAAK,MAAM;CACvF,CAAC;CAED,OAAO;AACT;;AAGA,MAAa,uBAAuB,SAAiC,QAAQ,MAAM,aAAa;;AAGhG,MAAa,cAAc,SAAoC;CAC7D,IAAI,QAA0B;CAC9B,MAAM,MAAM,UAAU,SAAS;EAC7B,QAAQ;EACR,OAAO;CACT,CAAC;CACD,OAAO;AACT"}
package/package.json ADDED
@@ -0,0 +1,96 @@
1
+ {
2
+ "name": "@cosense-toolbox/parser",
3
+ "version": "0.1.0-beta.0",
4
+ "description": "Cosense (Scrapbox) notation parser — position-aware, unist-style AST. Zero runtime deps except effect.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "sideEffects": false,
8
+ "engines": {
9
+ "node": ">=20"
10
+ },
11
+ "types": "./dist/index.d.cts",
12
+ "main": "./dist/index.cjs",
13
+ "module": "./dist/index.mjs",
14
+ "exports": {
15
+ ".": {
16
+ "import": {
17
+ "types": "./dist/index.d.mts",
18
+ "default": "./dist/index.mjs"
19
+ },
20
+ "require": {
21
+ "types": "./dist/index.d.cts",
22
+ "default": "./dist/index.cjs"
23
+ }
24
+ },
25
+ "./schema": {
26
+ "import": {
27
+ "types": "./dist/schema.d.mts",
28
+ "default": "./dist/schema.mjs"
29
+ },
30
+ "require": {
31
+ "types": "./dist/schema.d.cts",
32
+ "default": "./dist/schema.cjs"
33
+ }
34
+ },
35
+ "./utils": {
36
+ "import": {
37
+ "types": "./dist/utils.d.mts",
38
+ "default": "./dist/utils.mjs"
39
+ },
40
+ "require": {
41
+ "types": "./dist/utils.d.cts",
42
+ "default": "./dist/utils.cjs"
43
+ }
44
+ },
45
+ "./plugin": {
46
+ "import": {
47
+ "types": "./dist/plugin.d.mts",
48
+ "default": "./dist/plugin.mjs"
49
+ },
50
+ "require": {
51
+ "types": "./dist/plugin.d.cts",
52
+ "default": "./dist/plugin.cjs"
53
+ }
54
+ },
55
+ "./compile": {
56
+ "import": {
57
+ "types": "./dist/compile.d.mts",
58
+ "default": "./dist/compile.mjs"
59
+ },
60
+ "require": {
61
+ "types": "./dist/compile.d.cts",
62
+ "default": "./dist/compile.cjs"
63
+ }
64
+ },
65
+ "./package.json": "./package.json"
66
+ },
67
+ "files": ["dist", "README.md", "LICENSE"],
68
+ "scripts": {
69
+ "build": "tsdown",
70
+ "dev": "tsdown --watch",
71
+ "typecheck": "tsc --noEmit",
72
+ "test": "vitest run",
73
+ "test:watch": "vitest",
74
+ "prepublishOnly": "bun run build"
75
+ },
76
+ "dependencies": {
77
+ "effect": "^3.22.1"
78
+ },
79
+ "devDependencies": {
80
+ "fast-check": "^3.23.1",
81
+ "tsdown": "^0.22.14",
82
+ "typescript": "^6.0.0",
83
+ "vitest": "^2.1.8"
84
+ },
85
+ "publishConfig": {
86
+ "access": "public",
87
+ "tag": "beta",
88
+ "registry": "https://registry.npmjs.org/"
89
+ },
90
+ "keywords": ["cosense", "scrapbox", "parser", "ast", "unist", "effect"],
91
+ "repository": {
92
+ "type": "git",
93
+ "url": "git+https://github.com/qaynam/cosense-toolbox.git",
94
+ "directory": "packages/parser"
95
+ }
96
+ }