@mark1russell7/guidebook-topics 0.0.0-stage → 0.1.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Mark Russell
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,51 @@
1
- # Temporary Holding Version
1
+ # @mark1russell7/guidebook-topics
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ The topics add-on of the guidebook. A topic is a subject that a reader can learn. The topics make a graph of prerequisites, and the print order comes from that graph.
4
+
5
+ ```sh
6
+ pnpm add @mark1russell7/guidebook-topics
7
+ ```
8
+
9
+ The add-on names no topic. The topics, their areas and the codes of `serves` are input data of the caller.
10
+
11
+ ## The record
12
+
13
+ | Field | What it holds |
14
+ | --- | --- |
15
+ | `id` | A stable id, with the area first, for example `water.boil` |
16
+ | `title` | The title of the topic |
17
+ | `area` | The area of the topic |
18
+ | `requires` | The `id` of each topic that a reader must know first |
19
+ | `units` | The `id` of each cited unit of the core, in the sequence of the text |
20
+ | `bundles` | The `id` of each offline bundle of the library add-on that holds more of the topic |
21
+ | `serves` | Optional: the needs or functions that the topic helps to keep |
22
+ | `pages` | Optional: the count of printed pages |
23
+
24
+ ## The topic gate
25
+
26
+ ```ts
27
+ import { checkTopics, printOrder, prerequisitesOf } from "@mark1russell7/guidebook-topics";
28
+
29
+ const findings = checkTopics(topics, { areas, units: unitIds, bundles: bundleIds });
30
+ const book = printOrder(topics); // each topic comes after each topic that it requires
31
+ const before = prerequisitesOf(topics, "water.store");
32
+ ```
33
+
34
+ The gate examines only the lists that the caller gives.
35
+
36
+ | Code | When |
37
+ | --- | --- |
38
+ | `duplicate-id` | Two topics have the same `id`. |
39
+ | `empty-title` | A topic has no title. |
40
+ | `unknown-area` | The area is not in `areas`. |
41
+ | `missing-topic` | A prerequisite is not a topic of the list. |
42
+ | `self-require` | A topic is a prerequisite of itself. |
43
+ | `cycle` | Some topics are prerequisites of each other in a cycle. One finding names all the topics of the cycle. |
44
+ | `missing-unit`, `missing-bundle` | A unit or a bundle is not in the list of the caller. |
45
+ | `unknown-serve` | A code of `serves` is not in `serves`. |
46
+ | `no-content` | A topic has no unit and no bundle. |
47
+ | `bad-pages` | The count of pages is not a whole number of 1 or more. |
48
+
49
+ `printOrder` keeps the input order of the topics of one level. It throws a `RangeError` on a cycle, so start `checkTopics` first. `cyclesOf` gives each cycle as a list of its topics.
50
+
51
+ The graph functions come from `@mark1russell7/collections`.
@@ -0,0 +1,50 @@
1
+ import { Finding } from "@mark1russell7/guidebook";
2
+ /** A topic: a subject that a reader can learn, with the topics to learn first. */
3
+ interface Topic {
4
+ /** A stable id, with the area first, for example `water.boil`. */
5
+ readonly id: string;
6
+ readonly title: string;
7
+ /** The area of the topic. The caller gives the list of the areas. */
8
+ readonly area: string;
9
+ /** The topics that a reader must know first. The graph of all `requires` has no cycle. */
10
+ readonly requires: readonly string[];
11
+ /** The `id` of each cited unit of the topic, in the sequence of the text. */
12
+ readonly units: readonly string[];
13
+ /** The `id` of each offline bundle that holds more of the topic. */
14
+ readonly bundles: readonly string[];
15
+ /** The needs or functions that the topic helps to keep. The caller gives their codes. */
16
+ readonly serves?: readonly string[];
17
+ /** The count of printed pages. */
18
+ readonly pages?: number;
19
+ }
20
+ /** The codes of the findings of the topic gate. */
21
+ export declare const TOPIC_CODES: readonly ["duplicate-id", "empty-title", "unknown-area", "missing-topic", "self-require", "cycle", "missing-unit", "missing-bundle", "unknown-serve", "no-content", "bad-pages"];
22
+ /** A code of a finding of the topic gate. */
23
+ type TopicCode = (typeof TOPIC_CODES)[number];
24
+ /** A finding of the topic gate. */
25
+ type TopicFinding = Finding<TopicCode, "topic">;
26
+ /** The known ids that the gate examines. Each list that is not given is not examined. */
27
+ interface TopicOptions {
28
+ readonly areas?: readonly string[];
29
+ readonly units?: readonly string[];
30
+ readonly bundles?: readonly string[];
31
+ readonly serves?: readonly string[];
32
+ }
33
+ /**
34
+ * This function gives the findings of a list of topics.
35
+ * Each topic needs a unique id, a title, known prerequisites, units and bundles, and some content: a unit or a bundle.
36
+ * Each cycle of prerequisites gets one finding with the topics of the cycle.
37
+ */
38
+ export declare function checkTopics(topics: readonly Topic[], options?: TopicOptions): readonly TopicFinding[];
39
+ /** This function gives each cycle of prerequisites as the list of its topics, in the order of the input. */
40
+ export declare function cyclesOf(topics: readonly Topic[]): readonly (readonly string[])[];
41
+ /**
42
+ * This function gives the topics in the print order: each topic comes after each of its prerequisites.
43
+ * The topics of one level keep the order of the input. A prerequisite that is not in the list is not an edge.
44
+ * A cycle throws a `RangeError`: start `checkTopics` first.
45
+ */
46
+ export declare function printOrder(topics: readonly Topic[]): readonly Topic[];
47
+ /** This function gives each topic that a reader must know before the topic `id`, in the print order. */
48
+ export declare function prerequisitesOf(topics: readonly Topic[], id: string): readonly Topic[];
49
+ export type { Topic, TopicCode, TopicFinding, TopicOptions };
50
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../src/topics.ts"],"mappings":";;UAQiB;;WAEN;WACA;;WAEA;;WAEA;;WAEA;;WAEA;;WAEA;;WAEA;;;qBAIE;;KAeD,oBAAoB;;KAGpB,eAAe,QAAQ;;UAGlB;WACN;WACA;WACA;WACA;;;;;;;wBAQK,YAAY,iBAAiB,SAAS,UAAS,wBAA6B;;wBAyC5E,SAAS,iBAAiB;;;;;;wBAkB1B,WAAW,iBAAiB,mBAAmB;;wBAS/C,gBAAgB,iBAAiB,SAAS,sBAAsB"}
package/build/index.js ADDED
@@ -0,0 +1,91 @@
1
+ import { buildLeveledDAG, getAncestors } from "@mark1russell7/collections/dag";
2
+ import { stronglyConnectedComponents } from "@mark1russell7/collections/graph";
3
+ // src/topics.ts
4
+ /** The codes of the findings of the topic gate. */
5
+ const TOPIC_CODES = [
6
+ "duplicate-id",
7
+ "empty-title",
8
+ "unknown-area",
9
+ "missing-topic",
10
+ "self-require",
11
+ "cycle",
12
+ "missing-unit",
13
+ "missing-bundle",
14
+ "unknown-serve",
15
+ "no-content",
16
+ "bad-pages"
17
+ ];
18
+ /**
19
+ * This function gives the findings of a list of topics.
20
+ * Each topic needs a unique id, a title, known prerequisites, units and bundles, and some content: a unit or a bundle.
21
+ * Each cycle of prerequisites gets one finding with the topics of the cycle.
22
+ */
23
+ function checkTopics(topics, options = {}) {
24
+ const out = [];
25
+ const error = (code, id, message) => {
26
+ out.push({
27
+ level: "error",
28
+ code,
29
+ record: "topic",
30
+ id,
31
+ message
32
+ });
33
+ };
34
+ const ids = /* @__PURE__ */ new Set();
35
+ for (const topic of topics) {
36
+ if (ids.has(topic.id)) error("duplicate-id", topic.id, `Two topics have the id "${topic.id}".`);
37
+ ids.add(topic.id);
38
+ }
39
+ const known = (list) => list === void 0 ? void 0 : new Set(list);
40
+ const areas = known(options.areas);
41
+ const units = known(options.units);
42
+ const bundles = known(options.bundles);
43
+ const serves = known(options.serves);
44
+ for (const topic of topics) {
45
+ if (topic.title.trim() === "") error("empty-title", topic.id, `The topic "${topic.id}" has no title.`);
46
+ if (areas !== void 0 && !areas.has(topic.area)) error("unknown-area", topic.id, `The topic "${topic.id}" has the unknown area "${topic.area}".`);
47
+ for (const need of topic.requires) if (need === topic.id) error("self-require", topic.id, `The topic "${topic.id}" requires itself.`);
48
+ else if (!ids.has(need)) error("missing-topic", topic.id, `The topic "${topic.id}" requires the unknown topic "${need}".`);
49
+ for (const unit of topic.units) if (units !== void 0 && !units.has(unit)) error("missing-unit", topic.id, `The topic "${topic.id}" names the unknown unit "${unit}".`);
50
+ for (const bundle of topic.bundles) if (bundles !== void 0 && !bundles.has(bundle)) error("missing-bundle", topic.id, `The topic "${topic.id}" names the unknown bundle "${bundle}".`);
51
+ for (const serve of topic.serves ?? []) if (serves !== void 0 && !serves.has(serve)) error("unknown-serve", topic.id, `The topic "${topic.id}" serves the unknown code "${serve}".`);
52
+ if (topic.units.length === 0 && topic.bundles.length === 0) error("no-content", topic.id, `The topic "${topic.id}" has no unit and no bundle.`);
53
+ if (topic.pages !== void 0 && (!Number.isSafeInteger(topic.pages) || topic.pages < 1)) error("bad-pages", topic.id, `The topic "${topic.id}" has ${topic.pages} pages. It needs a whole number of 1 or more.`);
54
+ }
55
+ for (const cycle of cyclesOf(topics)) error("cycle", cycle[0] ?? "", `The topics ${cycle.map((id) => `"${id}"`).join(", ")} require each other in a cycle.`);
56
+ return out;
57
+ }
58
+ /** This function gives each cycle of prerequisites as the list of its topics, in the order of the input. */
59
+ function cyclesOf(topics) {
60
+ const requires = new Map(topics.map((topic) => [topic.id, topic.requires.filter((need) => need !== topic.id)]));
61
+ const components = stronglyConnectedComponents([...requires.keys()], (id) => requires.get(id) ?? []);
62
+ const order = new Map(topics.map((topic, index) => [topic.id, index]));
63
+ return components.filter((component) => component.length > 1).sort((a, b) => (order.get(a[0] ?? "") ?? 0) - (order.get(b[0] ?? "") ?? 0));
64
+ }
65
+ function nodesOf(topics) {
66
+ return new Map(topics.map((topic) => [topic.id, {
67
+ id: topic.id,
68
+ dependencies: [...topic.requires],
69
+ data: topic
70
+ }]));
71
+ }
72
+ /**
73
+ * This function gives the topics in the print order: each topic comes after each of its prerequisites.
74
+ * The topics of one level keep the order of the input. A prerequisite that is not in the list is not an edge.
75
+ * A cycle throws a `RangeError`: start `checkTopics` first.
76
+ */
77
+ function printOrder(topics) {
78
+ const cycles = cyclesOf(topics);
79
+ if (cycles.length > 0) throw new RangeError(`printOrder: the topics ${cycles[0]?.join(", ") ?? ""} require each other in a cycle.`);
80
+ const dag = buildLeveledDAG(nodesOf(topics));
81
+ const order = new Map(topics.map((topic, index) => [topic.id, index]));
82
+ return dag.levels.flatMap((level) => [...level].sort((a, b) => (order.get(a.id) ?? 0) - (order.get(b.id) ?? 0)).map((node) => node.data));
83
+ }
84
+ /** This function gives each topic that a reader must know before the topic `id`, in the print order. */
85
+ function prerequisitesOf(topics, id) {
86
+ const before = getAncestors(nodesOf(topics), id);
87
+ return printOrder(topics).filter((topic) => before.has(topic.id));
88
+ }
89
+ export { TOPIC_CODES, checkTopics, cyclesOf, prerequisitesOf, printOrder };
90
+
91
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/topics.ts"],"sourcesContent":["// The topics add-on: a topic is one node of a graph of prerequisites. The add-on names no topic.\n\nimport { buildLeveledDAG, getAncestors } from \"@mark1russell7/collections/dag\";\nimport type { DAGNode } from \"@mark1russell7/collections/dag\";\nimport { stronglyConnectedComponents } from \"@mark1russell7/collections/graph\";\nimport type { Finding } from \"@mark1russell7/guidebook\";\n\n/** A topic: a subject that a reader can learn, with the topics to learn first. */\nexport interface Topic {\n /** A stable id, with the area first, for example `water.boil`. */\n readonly id: string;\n readonly title: string;\n /** The area of the topic. The caller gives the list of the areas. */\n readonly area: string;\n /** The topics that a reader must know first. The graph of all `requires` has no cycle. */\n readonly requires: readonly string[];\n /** The `id` of each cited unit of the topic, in the sequence of the text. */\n readonly units: readonly string[];\n /** The `id` of each offline bundle that holds more of the topic. */\n readonly bundles: readonly string[];\n /** The needs or functions that the topic helps to keep. The caller gives their codes. */\n readonly serves?: readonly string[];\n /** The count of printed pages. */\n readonly pages?: number;\n}\n\n/** The codes of the findings of the topic gate. */\nexport const TOPIC_CODES = [\n \"duplicate-id\",\n \"empty-title\",\n \"unknown-area\",\n \"missing-topic\",\n \"self-require\",\n \"cycle\",\n \"missing-unit\",\n \"missing-bundle\",\n \"unknown-serve\",\n \"no-content\",\n \"bad-pages\",\n] as const;\n\n/** A code of a finding of the topic gate. */\nexport type TopicCode = (typeof TOPIC_CODES)[number];\n\n/** A finding of the topic gate. */\nexport type TopicFinding = Finding<TopicCode, \"topic\">;\n\n/** The known ids that the gate examines. Each list that is not given is not examined. */\nexport interface TopicOptions {\n readonly areas?: readonly string[];\n readonly units?: readonly string[];\n readonly bundles?: readonly string[];\n readonly serves?: readonly string[];\n}\n\n/**\n * This function gives the findings of a list of topics.\n * Each topic needs a unique id, a title, known prerequisites, units and bundles, and some content: a unit or a bundle.\n * Each cycle of prerequisites gets one finding with the topics of the cycle.\n */\nexport function checkTopics(topics: readonly Topic[], options: TopicOptions = {}): readonly TopicFinding[] {\n const out: TopicFinding[] = [];\n const error = (code: TopicCode, id: string, message: string): void => {\n out.push({ level: \"error\", code, record: \"topic\", id, message });\n };\n const ids = new Set<string>();\n for (const topic of topics) {\n if (ids.has(topic.id)) error(\"duplicate-id\", topic.id, `Two topics have the id \"${topic.id}\".`);\n ids.add(topic.id);\n }\n const known = (list: readonly string[] | undefined): Set<string> | undefined => (list === undefined ? undefined : new Set(list));\n const areas = known(options.areas);\n const units = known(options.units);\n const bundles = known(options.bundles);\n const serves = known(options.serves);\n for (const topic of topics) {\n if (topic.title.trim() === \"\") error(\"empty-title\", topic.id, `The topic \"${topic.id}\" has no title.`);\n if (areas !== undefined && !areas.has(topic.area)) error(\"unknown-area\", topic.id, `The topic \"${topic.id}\" has the unknown area \"${topic.area}\".`);\n for (const need of topic.requires) {\n if (need === topic.id) error(\"self-require\", topic.id, `The topic \"${topic.id}\" requires itself.`);\n else if (!ids.has(need)) error(\"missing-topic\", topic.id, `The topic \"${topic.id}\" requires the unknown topic \"${need}\".`);\n }\n for (const unit of topic.units) {\n if (units !== undefined && !units.has(unit)) error(\"missing-unit\", topic.id, `The topic \"${topic.id}\" names the unknown unit \"${unit}\".`);\n }\n for (const bundle of topic.bundles) {\n if (bundles !== undefined && !bundles.has(bundle)) error(\"missing-bundle\", topic.id, `The topic \"${topic.id}\" names the unknown bundle \"${bundle}\".`);\n }\n for (const serve of topic.serves ?? []) {\n if (serves !== undefined && !serves.has(serve)) error(\"unknown-serve\", topic.id, `The topic \"${topic.id}\" serves the unknown code \"${serve}\".`);\n }\n if (topic.units.length === 0 && topic.bundles.length === 0) error(\"no-content\", topic.id, `The topic \"${topic.id}\" has no unit and no bundle.`);\n if (topic.pages !== undefined && (!Number.isSafeInteger(topic.pages) || topic.pages < 1)) error(\"bad-pages\", topic.id, `The topic \"${topic.id}\" has ${topic.pages} pages. It needs a whole number of 1 or more.`);\n }\n for (const cycle of cyclesOf(topics)) {\n error(\"cycle\", cycle[0] ?? \"\", `The topics ${cycle.map((id) => `\"${id}\"`).join(\", \")} require each other in a cycle.`);\n }\n return out;\n}\n\n/** This function gives each cycle of prerequisites as the list of its topics, in the order of the input. */\nexport function cyclesOf(topics: readonly Topic[]): readonly (readonly string[])[] {\n const requires = new Map(topics.map((topic) => [topic.id, topic.requires.filter((need) => need !== topic.id)]));\n const components = stronglyConnectedComponents([...requires.keys()], (id) => requires.get(id) ?? []);\n const order = new Map(topics.map((topic, index) => [topic.id, index]));\n return components\n .filter((component) => component.length > 1)\n .sort((a, b) => (order.get(a[0] ?? \"\") ?? 0) - (order.get(b[0] ?? \"\") ?? 0));\n}\n\nfunction nodesOf(topics: readonly Topic[]): Map<string, DAGNode<Topic>> {\n return new Map(topics.map((topic) => [topic.id, { id: topic.id, dependencies: [...topic.requires], data: topic }]));\n}\n\n/**\n * This function gives the topics in the print order: each topic comes after each of its prerequisites.\n * The topics of one level keep the order of the input. A prerequisite that is not in the list is not an edge.\n * A cycle throws a `RangeError`: start `checkTopics` first.\n */\nexport function printOrder(topics: readonly Topic[]): readonly Topic[] {\n const cycles = cyclesOf(topics);\n if (cycles.length > 0) throw new RangeError(`printOrder: the topics ${cycles[0]?.join(\", \") ?? \"\"} require each other in a cycle.`);\n const dag = buildLeveledDAG(nodesOf(topics));\n const order = new Map(topics.map((topic, index) => [topic.id, index]));\n return dag.levels.flatMap((level) => [...level].sort((a, b) => (order.get(a.id) ?? 0) - (order.get(b.id) ?? 0)).map((node) => node.data as Topic));\n}\n\n/** This function gives each topic that a reader must know before the topic `id`, in the print order. */\nexport function prerequisitesOf(topics: readonly Topic[], id: string): readonly Topic[] {\n const before = getAncestors(nodesOf(topics), id);\n return printOrder(topics).filter((topic) => before.has(topic.id));\n}\n"],"mappings":";;;;AA2BA,MAAa,cAAc;CACzB;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF;;;;;;AAqBA,SAAgB,YAAY,QAA0B,UAAwB,CAAC,GAA4B;CACzG,MAAM,MAAsB,CAAC;CAC7B,MAAM,SAAS,MAAiB,IAAY,YAA0B;EACpE,IAAI,KAAK;GAAE,OAAO;GAAS;GAAM,QAAQ;GAAS;GAAI;EAAQ,CAAC;CACjE;CACA,MAAM,sBAAM,IAAI,IAAY;CAC5B,KAAK,MAAM,SAAS,QAAQ;EAC1B,IAAI,IAAI,IAAI,MAAM,EAAE,GAAG,MAAM,gBAAgB,MAAM,IAAI,2BAA2B,MAAM,GAAG,GAAG;EAC9F,IAAI,IAAI,MAAM,EAAE;CAClB;CACA,MAAM,SAAS,SAAkE,SAAS,KAAA,IAAY,KAAA,IAAY,IAAI,IAAI,IAAI;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AM,MAAM,gBAAgB,QAAQ,MAAM,CAAC;CAC3C,MAAM,QAAQ,IAAI,IAAI,OAAO,KAAK,OAAO,UAAU,CAAC,MAAM,IAAI,KAAK,CAAC,CAAC;CACrE,OAAO,IAAI,OAAO,SAAS,UAAU,CAAC,GAAG,KAAK,CAAC,CAAC,MAAM,GAAG,OAAO,MAAM,IAAI,EAAE,EAAE,KAAK,MAAM,MAAM,IAAI,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC,KAAK,SAAS,KAAK,IAAa,CAAC;AACnJ;;AAGA,SAAgB,gBAAgB,QAA0B,IAA8B;CACtF,MAAM,SAAS,aAAa,QAAQ,MAAM,GAAG,EAAE;CAC/C,OAAO,WAAW,MAAM,CAAC,CAAC,QAAQ,UAAU,OAAO,IAAI,MAAM,EAAE,CAAC;AAClE"}
package/package.json CHANGED
@@ -1,6 +1,66 @@
1
1
  {
2
+ "$schema": "https://json.schemastore.org/package",
2
3
  "name": "@mark1russell7/guidebook-topics",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
4
+ "version": "0.1.0",
5
+ "description": "The topics add-on of the guidebook: topics as a graph of prerequisites, the graph gate (no cycle, no unknown topic, unit or bundle) and the print order. The add-on names no topic.",
6
+ "keywords": [
7
+ "guidebook",
8
+ "topics",
9
+ "prerequisites",
10
+ "dag",
11
+ "curriculum",
12
+ "knowledge-graph",
13
+ "typescript"
14
+ ],
15
+ "homepage": "https://github.com/mark1russell7/guidebook#readme",
16
+ "bugs": {
17
+ "url": "https://github.com/mark1russell7/guidebook/issues"
18
+ },
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "git+https://github.com/mark1russell7/guidebook.git",
22
+ "directory": "packages/topics"
23
+ },
24
+ "license": "MIT",
25
+ "author": "Mark Russell (https://github.com/mark1russell7)",
26
+ "type": "module",
27
+ "sideEffects": false,
28
+ "main": "./build/index.js",
29
+ "types": "./build/index.d.ts",
30
+ "exports": {
31
+ ".": {
32
+ "types": "./build/index.d.ts",
33
+ "default": "./build/index.js"
34
+ },
35
+ "./package.json": "./package.json"
36
+ },
37
+ "files": [
38
+ "build",
39
+ "src",
40
+ "!src/**/*.test.ts"
41
+ ],
42
+ "engines": {
43
+ "node": ">=22.12"
44
+ },
45
+ "publishConfig": {
46
+ "access": "public"
47
+ },
48
+ "dependencies": {
49
+ "@mark1russell7/collections": "^0.11.3",
50
+ "@mark1russell7/guidebook": "0.1.0"
51
+ },
52
+ "devDependencies": {
53
+ "@mark1russell7/cue": "^2.0.0",
54
+ "@types/node": "^26.6.5",
55
+ "@vitest/coverage-v8": "^5.0.3",
56
+ "typescript": "^7.0.2",
57
+ "vitest": "^5.0.3",
58
+ "tsdown": "^0.23.7"
59
+ },
60
+ "scripts": {
61
+ "build": "tsdown",
62
+ "test": "vitest run",
63
+ "test:watch": "vitest",
64
+ "typecheck": "tsc -p tsconfig.json && tsc -p tsconfig.test.json"
65
+ }
6
66
  }
package/src/index.ts ADDED
@@ -0,0 +1,5 @@
1
+ // The topics add-on of the guidebook: topics as a graph of prerequisites, the topic gate and the print order.
2
+ // The add-on names no topic: the topics and their areas are input data of the caller.
3
+
4
+ export { TOPIC_CODES, checkTopics, cyclesOf, prerequisitesOf, printOrder } from "./topics.ts";
5
+ export type { Topic, TopicCode, TopicFinding, TopicOptions } from "./topics.ts";
package/src/topics.ts ADDED
@@ -0,0 +1,132 @@
1
+ // The topics add-on: a topic is one node of a graph of prerequisites. The add-on names no topic.
2
+
3
+ import { buildLeveledDAG, getAncestors } from "@mark1russell7/collections/dag";
4
+ import type { DAGNode } from "@mark1russell7/collections/dag";
5
+ import { stronglyConnectedComponents } from "@mark1russell7/collections/graph";
6
+ import type { Finding } from "@mark1russell7/guidebook";
7
+
8
+ /** A topic: a subject that a reader can learn, with the topics to learn first. */
9
+ export interface Topic {
10
+ /** A stable id, with the area first, for example `water.boil`. */
11
+ readonly id: string;
12
+ readonly title: string;
13
+ /** The area of the topic. The caller gives the list of the areas. */
14
+ readonly area: string;
15
+ /** The topics that a reader must know first. The graph of all `requires` has no cycle. */
16
+ readonly requires: readonly string[];
17
+ /** The `id` of each cited unit of the topic, in the sequence of the text. */
18
+ readonly units: readonly string[];
19
+ /** The `id` of each offline bundle that holds more of the topic. */
20
+ readonly bundles: readonly string[];
21
+ /** The needs or functions that the topic helps to keep. The caller gives their codes. */
22
+ readonly serves?: readonly string[];
23
+ /** The count of printed pages. */
24
+ readonly pages?: number;
25
+ }
26
+
27
+ /** The codes of the findings of the topic gate. */
28
+ export const TOPIC_CODES = [
29
+ "duplicate-id",
30
+ "empty-title",
31
+ "unknown-area",
32
+ "missing-topic",
33
+ "self-require",
34
+ "cycle",
35
+ "missing-unit",
36
+ "missing-bundle",
37
+ "unknown-serve",
38
+ "no-content",
39
+ "bad-pages",
40
+ ] as const;
41
+
42
+ /** A code of a finding of the topic gate. */
43
+ export type TopicCode = (typeof TOPIC_CODES)[number];
44
+
45
+ /** A finding of the topic gate. */
46
+ export type TopicFinding = Finding<TopicCode, "topic">;
47
+
48
+ /** The known ids that the gate examines. Each list that is not given is not examined. */
49
+ export interface TopicOptions {
50
+ readonly areas?: readonly string[];
51
+ readonly units?: readonly string[];
52
+ readonly bundles?: readonly string[];
53
+ readonly serves?: readonly string[];
54
+ }
55
+
56
+ /**
57
+ * This function gives the findings of a list of topics.
58
+ * Each topic needs a unique id, a title, known prerequisites, units and bundles, and some content: a unit or a bundle.
59
+ * Each cycle of prerequisites gets one finding with the topics of the cycle.
60
+ */
61
+ export function checkTopics(topics: readonly Topic[], options: TopicOptions = {}): readonly TopicFinding[] {
62
+ const out: TopicFinding[] = [];
63
+ const error = (code: TopicCode, id: string, message: string): void => {
64
+ out.push({ level: "error", code, record: "topic", id, message });
65
+ };
66
+ const ids = new Set<string>();
67
+ for (const topic of topics) {
68
+ if (ids.has(topic.id)) error("duplicate-id", topic.id, `Two topics have the id "${topic.id}".`);
69
+ ids.add(topic.id);
70
+ }
71
+ const known = (list: readonly string[] | undefined): Set<string> | undefined => (list === undefined ? undefined : new Set(list));
72
+ const areas = known(options.areas);
73
+ const units = known(options.units);
74
+ const bundles = known(options.bundles);
75
+ const serves = known(options.serves);
76
+ for (const topic of topics) {
77
+ if (topic.title.trim() === "") error("empty-title", topic.id, `The topic "${topic.id}" has no title.`);
78
+ if (areas !== undefined && !areas.has(topic.area)) error("unknown-area", topic.id, `The topic "${topic.id}" has the unknown area "${topic.area}".`);
79
+ for (const need of topic.requires) {
80
+ if (need === topic.id) error("self-require", topic.id, `The topic "${topic.id}" requires itself.`);
81
+ else if (!ids.has(need)) error("missing-topic", topic.id, `The topic "${topic.id}" requires the unknown topic "${need}".`);
82
+ }
83
+ for (const unit of topic.units) {
84
+ if (units !== undefined && !units.has(unit)) error("missing-unit", topic.id, `The topic "${topic.id}" names the unknown unit "${unit}".`);
85
+ }
86
+ for (const bundle of topic.bundles) {
87
+ if (bundles !== undefined && !bundles.has(bundle)) error("missing-bundle", topic.id, `The topic "${topic.id}" names the unknown bundle "${bundle}".`);
88
+ }
89
+ for (const serve of topic.serves ?? []) {
90
+ if (serves !== undefined && !serves.has(serve)) error("unknown-serve", topic.id, `The topic "${topic.id}" serves the unknown code "${serve}".`);
91
+ }
92
+ if (topic.units.length === 0 && topic.bundles.length === 0) error("no-content", topic.id, `The topic "${topic.id}" has no unit and no bundle.`);
93
+ if (topic.pages !== undefined && (!Number.isSafeInteger(topic.pages) || topic.pages < 1)) error("bad-pages", topic.id, `The topic "${topic.id}" has ${topic.pages} pages. It needs a whole number of 1 or more.`);
94
+ }
95
+ for (const cycle of cyclesOf(topics)) {
96
+ error("cycle", cycle[0] ?? "", `The topics ${cycle.map((id) => `"${id}"`).join(", ")} require each other in a cycle.`);
97
+ }
98
+ return out;
99
+ }
100
+
101
+ /** This function gives each cycle of prerequisites as the list of its topics, in the order of the input. */
102
+ export function cyclesOf(topics: readonly Topic[]): readonly (readonly string[])[] {
103
+ const requires = new Map(topics.map((topic) => [topic.id, topic.requires.filter((need) => need !== topic.id)]));
104
+ const components = stronglyConnectedComponents([...requires.keys()], (id) => requires.get(id) ?? []);
105
+ const order = new Map(topics.map((topic, index) => [topic.id, index]));
106
+ return components
107
+ .filter((component) => component.length > 1)
108
+ .sort((a, b) => (order.get(a[0] ?? "") ?? 0) - (order.get(b[0] ?? "") ?? 0));
109
+ }
110
+
111
+ function nodesOf(topics: readonly Topic[]): Map<string, DAGNode<Topic>> {
112
+ return new Map(topics.map((topic) => [topic.id, { id: topic.id, dependencies: [...topic.requires], data: topic }]));
113
+ }
114
+
115
+ /**
116
+ * This function gives the topics in the print order: each topic comes after each of its prerequisites.
117
+ * The topics of one level keep the order of the input. A prerequisite that is not in the list is not an edge.
118
+ * A cycle throws a `RangeError`: start `checkTopics` first.
119
+ */
120
+ export function printOrder(topics: readonly Topic[]): readonly Topic[] {
121
+ const cycles = cyclesOf(topics);
122
+ if (cycles.length > 0) throw new RangeError(`printOrder: the topics ${cycles[0]?.join(", ") ?? ""} require each other in a cycle.`);
123
+ const dag = buildLeveledDAG(nodesOf(topics));
124
+ const order = new Map(topics.map((topic, index) => [topic.id, index]));
125
+ return dag.levels.flatMap((level) => [...level].sort((a, b) => (order.get(a.id) ?? 0) - (order.get(b.id) ?? 0)).map((node) => node.data as Topic));
126
+ }
127
+
128
+ /** This function gives each topic that a reader must know before the topic `id`, in the print order. */
129
+ export function prerequisitesOf(topics: readonly Topic[], id: string): readonly Topic[] {
130
+ const before = getAncestors(nodesOf(topics), id);
131
+ return printOrder(topics).filter((topic) => before.has(topic.id));
132
+ }