embedex 0.1.1 → 0.3.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/README.md +75 -44
- package/package.json +1 -1
- package/src/bin/cli.js +1 -1
- package/src/bin/cli.js.map +1 -1
- package/src/bin/processResult.js +5 -7
- package/src/bin/processResult.js.map +1 -1
- package/src/lib/internal/processTargets.js +64 -44
- package/src/lib/internal/processTargets.js.map +1 -1
- package/src/lib/internal/types.d.ts +1 -2
package/README.md
CHANGED
|
@@ -1,12 +1,8 @@
|
|
|
1
1
|
# embedex <!-- omit from toc -->
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
You can write code directly in TypeDoc comments using the [`@example` tag](https://typedoc.org/tags/example/), but keeping them up to date and guaranteed runnable is challenging since they aren't type-checked, linted, or tested.
|
|
6
|
-
|
|
7
|
-
While [`typedoc-plugin-include-example`](https://github.com/ferdodo/typedoc-plugin-include-example) embeds code into the resulting TypeDoc, the examples aren't in your code, so IDEs cannot show them on hover.
|
|
3
|
+
Embed shared text and code snippets from source files into destination files. For example, embed TypeScript examples into TypeDoc comments and your README.
|
|
8
4
|
|
|
9
|
-
|
|
5
|
+
`embedex` helps ensure a single source of truth while ensuring examples are up-to-date with the code they are documenting, runnable, linted, tested, and show on hover in IDEs.
|
|
10
6
|
|
|
11
7
|
## Table of contents <!-- omit from toc -->
|
|
12
8
|
|
|
@@ -31,50 +27,85 @@ npm install --global embedex
|
|
|
31
27
|
|
|
32
28
|
## Usage
|
|
33
29
|
|
|
34
|
-
1. Add
|
|
30
|
+
1. Add a source file to the `./examples` directory (configurable). The first line is a comma-separated list of destination file paths to embed the source's file contents into. `./examples/greeter.ts`:
|
|
35
31
|
|
|
36
32
|
```ts
|
|
37
|
-
// src/greeter.ts
|
|
33
|
+
// README.md,src/greeter.ts
|
|
38
34
|
import { greet } from "@my-scope/greeter";
|
|
39
35
|
|
|
40
36
|
greet("world");
|
|
41
37
|
```
|
|
42
38
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
39
|
+
2. In the destination file, add an `<embedex source="..."></embedex>` tag that includes the source file's path.
|
|
40
|
+
|
|
41
|
+
1. `./README.md`:
|
|
42
|
+
|
|
43
|
+
```
|
|
44
|
+
# greeter
|
|
45
|
+
|
|
46
|
+
Greets a person by name.
|
|
47
|
+
|
|
48
|
+
<embedex source="examples/greeter.ts">
|
|
49
|
+
</embedex>
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
2. `./src/greeter.ts`:
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
/**
|
|
56
|
+
* Greets a person by name.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* <embedex source="examples/greeter.ts">
|
|
60
|
+
* </embedex>
|
|
61
|
+
*/
|
|
62
|
+
function greet(name: string) {
|
|
63
|
+
console.log(`Hello, ${name}!`);
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
3. Run `npx embedex`.
|
|
68
|
+
4. The example is embedded! `./src/greeter.ts`:
|
|
69
|
+
|
|
70
|
+
1. `./README.md`:
|
|
71
|
+
|
|
72
|
+
````
|
|
73
|
+
# greeter
|
|
74
|
+
|
|
75
|
+
Greets a person by name.
|
|
76
|
+
|
|
77
|
+
<embedex source="examples/greeter.ts">
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
import { greet } from "@my-scope/greeter";
|
|
81
|
+
|
|
82
|
+
greet("world");
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
</embedex>
|
|
86
|
+
````
|
|
87
|
+
|
|
88
|
+
2. `./src/greeter.ts`:
|
|
89
|
+
|
|
90
|
+
````ts
|
|
91
|
+
/**
|
|
92
|
+
* Greets a person by name.
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* <embedex source="examples/greeter.ts">
|
|
96
|
+
*
|
|
97
|
+
* ```ts
|
|
98
|
+
* import { greet } from "@my-scope/greeter";
|
|
99
|
+
*
|
|
100
|
+
* greet("world");
|
|
101
|
+
* ```
|
|
102
|
+
*
|
|
103
|
+
* </embedex>
|
|
104
|
+
*/
|
|
105
|
+
function greet(name: string) {
|
|
106
|
+
console.log(`Hello, ${name}!`);
|
|
107
|
+
}
|
|
108
|
+
````
|
|
78
109
|
|
|
79
110
|
## Reference
|
|
80
111
|
|
|
@@ -85,7 +116,7 @@ A command-line interface (CLI) that embeds examples into TypeDoc comments.
|
|
|
85
116
|
|
|
86
117
|
Options:
|
|
87
118
|
-V, --version output the version number
|
|
88
|
-
-e, --examplesGlob <pattern> examples glob pattern (default: "examples/**/*.ts")
|
|
119
|
+
-e, --examplesGlob <pattern> examples glob pattern (default: "examples/**/*.{md,ts}")
|
|
89
120
|
-c, --check verify if examples are correctly embedded without making changes,
|
|
90
121
|
exits with non-zero code if updates are needed; useful for CI/CD
|
|
91
122
|
pipelines (default: false)
|
package/package.json
CHANGED
package/src/bin/cli.js
CHANGED
|
@@ -9,7 +9,7 @@ const program = new extra_typings_1.Command()
|
|
|
9
9
|
.name(package_json_1.name)
|
|
10
10
|
.description(package_json_1.description)
|
|
11
11
|
.version(String(package_json_1.version))
|
|
12
|
-
.addOption(new extra_typings_1.Option("-e, --examplesGlob <pattern>", "examples glob pattern").default("examples/**/*.ts"))
|
|
12
|
+
.addOption(new extra_typings_1.Option("-e, --examplesGlob <pattern>", "examples glob pattern").default("examples/**/*.{md,ts}"))
|
|
13
13
|
.addOption(new extra_typings_1.Option("-c, --check", "verify if examples are correctly embedded without making changes, exits with non-zero code if updates are needed; useful for CI/CD pipelines").default(false))
|
|
14
14
|
.addOption(new extra_typings_1.Option("-v, --verbose", "show verbose output").default(false));
|
|
15
15
|
program.parse();
|
package/src/bin/cli.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../../../../../packages/embedex/src/bin/cli.ts"],"names":[],"mappings":";;;AACA,+DAA8D;AAE9D,qDAAgE;AAChE,wCAAqC;AACrC,mDAAqD;AAErD,MAAM,OAAO,GAAG,IAAI,uBAAO,EAAE;KAC1B,IAAI,CAAC,mBAAI,CAAC;KACV,WAAW,CAAC,0BAAW,CAAC;KACxB,OAAO,CAAC,MAAM,CAAC,sBAAO,CAAC,CAAC;KACxB,SAAS,CACR,IAAI,sBAAM,CAAC,8BAA8B,EAAE,uBAAuB,CAAC,CAAC,OAAO,
|
|
1
|
+
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../../../../../packages/embedex/src/bin/cli.ts"],"names":[],"mappings":";;;AACA,+DAA8D;AAE9D,qDAAgE;AAChE,wCAAqC;AACrC,mDAAqD;AAErD,MAAM,OAAO,GAAG,IAAI,uBAAO,EAAE;KAC1B,IAAI,CAAC,mBAAI,CAAC;KACV,WAAW,CAAC,0BAAW,CAAC;KACxB,OAAO,CAAC,MAAM,CAAC,sBAAO,CAAC,CAAC;KACxB,SAAS,CACR,IAAI,sBAAM,CAAC,8BAA8B,EAAE,uBAAuB,CAAC,CAAC,OAAO,CACzE,uBAAuB,CACxB,CACF;KACA,SAAS,CACR,IAAI,sBAAM,CACR,aAAa,EACb,8IAA8I,CAC/I,CAAC,OAAO,CAAC,KAAK,CAAC,CACjB;KACA,SAAS,CAAC,IAAI,sBAAM,CAAC,eAAe,EAAE,qBAAqB,CAAC,CAAC,OAAO,CAAC,KAA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EAAE;IACf,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;IAC7B,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAClB,CAAC,CAAC,CAAC"}
|
package/src/bin/processResult.js
CHANGED
|
@@ -30,22 +30,20 @@ function processResult(params) {
|
|
|
30
30
|
code,
|
|
31
31
|
paths: { target: relative(target), examples: examples.map((path) => relative(path)) },
|
|
32
32
|
});
|
|
33
|
+
// eslint-disable-next-line default-case -- ignore so we get @typescript-eslint/switch-exhaustiveness-check
|
|
33
34
|
switch (code) {
|
|
34
35
|
case "NO_CHANGE": {
|
|
35
|
-
output.push(toOutput());
|
|
36
|
+
output.push(toOutput({ isError: false }));
|
|
36
37
|
break;
|
|
37
38
|
}
|
|
38
39
|
case "NO_MATCH": {
|
|
39
|
-
output.push(toOutput(true));
|
|
40
|
+
output.push(toOutput({ isError: true }));
|
|
40
41
|
break;
|
|
41
42
|
}
|
|
42
43
|
case "UPDATE": {
|
|
43
|
-
output.push(toOutput(check));
|
|
44
|
+
output.push(toOutput({ isError: check }));
|
|
44
45
|
break;
|
|
45
46
|
}
|
|
46
|
-
default: {
|
|
47
|
-
throw new Error(`Unknown embed ${JSON.stringify(embed)}`);
|
|
48
|
-
}
|
|
49
47
|
}
|
|
50
48
|
}
|
|
51
49
|
return output.sort((a, b) => a.code.localeCompare(b.code));
|
|
@@ -53,7 +51,7 @@ function processResult(params) {
|
|
|
53
51
|
function createToOutput(params) {
|
|
54
52
|
const { code, paths } = params;
|
|
55
53
|
const { target, examples } = paths;
|
|
56
|
-
return (isError
|
|
54
|
+
return ({ isError }) => ({
|
|
57
55
|
code,
|
|
58
56
|
isError,
|
|
59
57
|
message: `${yoctocolors_cjs_1.default.green(code)} ${yoctocolors_cjs_1.default.gray(target)} -> ${yoctocolors_cjs_1.default.gray(examples.join(", "))}`,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"processResult.js","sourceRoot":"","sources":["../../../../../packages/embedex/src/bin/processResult.ts"],"names":[],"mappings":";;AAYA,kBAEC;AAED,
|
|
1
|
+
{"version":3,"file":"processResult.js","sourceRoot":"","sources":["../../../../../packages/embedex/src/bin/processResult.ts"],"names":[],"mappings":";;AAYA,kBAEC;AAED,sCA6DC;;AA7ED,yCAAqD;AAErD,8EAAqC;AAUrC,SAAgB,GAAG,CAAC,GAAG,QAAkB;IACvC,OAAO,yBAAM,CAAC,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;AACxC,CAAC;AAED,SAAgB,aAAa,CAAC,MAK7B;IACC,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,OAAO,EAAE,GAAG,MAAM,CAAC;IAC/C,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,OAAO,EAAE,GAAG,MAAM,CAAC;IAE7C,SAAS,QAAQ,CAAC,IAAY;QAC5B,OAAO,IAAA,oBAAY,EAAC,GAAG,EAAE,IAAI,CAAC,CAAC;IACjC,CAAC;IAED,IAAI,OAAO,EAAE,CAAC;QACZ,OAAO,CAAC,GAAG,CACT,GAAG,CACD,eAAe,EACf,QAAQ;aACL,GAAG,CAAC,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC,GAAG,QAAQ,CAAC,IAAI,CAAC,OAAO,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;aACtF,IAAI,CAAC,MAAM,CAAC,CAChB,CACF,CAAC;QACF,OAAO,CAAC,GAAG,CACT,GAAG,CACD,cAAc,EACd,OAAO;aACJ,GAAG,CAAC,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,EAAE,EAAE,CAAC,GAAG,QAAQ,CAAC,IAAI,CAAC,OAAO,QAAQ,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;aACxF,IAAI,CAAC,MA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EAAE,KAAK,EAAE,GAAG,MAAM,CAAC;IAC/B,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,KAAK,CAAC;IAEnC,OAAO,CAAC,EAAE,OAAO,EAAwB,EAAE,EAAE,CAAC,CAAC;QAC7C,IAAI;QACJ,OAAO;QACP,OAAO,EAAE,GAAG,yBAAM,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,yBAAM,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,yBAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,EAAE;KAC/F,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -2,64 +2,84 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.processTargets = processTargets;
|
|
4
4
|
const node_path_1 = require("node:path");
|
|
5
|
+
const CODE_FENCE_ID_BY_FILE_EXTENSION = {
|
|
6
|
+
cjs: "js",
|
|
7
|
+
cts: "ts",
|
|
8
|
+
js: "js",
|
|
9
|
+
jsx: "js",
|
|
10
|
+
md: "",
|
|
11
|
+
mdx: "",
|
|
12
|
+
mjs: "js",
|
|
13
|
+
mts: "ts",
|
|
14
|
+
ts: "ts",
|
|
15
|
+
tsx: "ts",
|
|
16
|
+
};
|
|
5
17
|
function processTargets(params) {
|
|
6
|
-
const {
|
|
18
|
+
const { targetMap, ...rest } = params;
|
|
19
|
+
const result = [];
|
|
20
|
+
for (const entry of targetMap.entries()) {
|
|
21
|
+
result.push(processTarget({ ...rest, entry }));
|
|
22
|
+
}
|
|
23
|
+
return result;
|
|
24
|
+
}
|
|
25
|
+
function processTarget(params) {
|
|
26
|
+
const { cwd, exampleMap, entry } = params;
|
|
27
|
+
const [target, { content, examples }] = entry;
|
|
7
28
|
function absolutePath(path) {
|
|
8
29
|
return (0, node_path_1.join)(cwd, path);
|
|
9
30
|
}
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
const matches = matchAll({ content, exists: (example) => examples.has(absolutePath(example)) });
|
|
14
|
-
if (matches.length === 0) {
|
|
15
|
-
result.push({ code: "NO_MATCH", paths: { target, examples: [] } });
|
|
16
|
-
}
|
|
17
|
-
else {
|
|
18
|
-
let updatedContent = content;
|
|
19
|
-
for (const { fullMatch, language, indent, example } of matches) {
|
|
20
|
-
const exampleContent = exampleMap.get(absolutePath(example));
|
|
21
|
-
const codeBlock = exampleContent.content.includes("```") ? "````" : "```";
|
|
22
|
-
const replacement = `${codeBlock}${language}\n${prefixLines({
|
|
23
|
-
content: [
|
|
24
|
-
`// ${example}`,
|
|
25
|
-
...exampleContent.content.replace("*/", "*\\/").split("\n"),
|
|
26
|
-
codeBlock,
|
|
27
|
-
],
|
|
28
|
-
indent,
|
|
29
|
-
})}`;
|
|
30
|
-
updatedContent = updatedContent.replaceAll(fullMatch, replacement);
|
|
31
|
-
}
|
|
32
|
-
const paths = { examples: matches.map((m) => absolutePath(m.example)), target };
|
|
33
|
-
result.push(content === updatedContent
|
|
34
|
-
? { code: "NO_CHANGE", paths }
|
|
35
|
-
: { code: "UPDATE", paths, updatedContent });
|
|
36
|
-
}
|
|
31
|
+
const matches = matchAll({ content, exists: (example) => examples.has(absolutePath(example)) });
|
|
32
|
+
if (matches.length === 0) {
|
|
33
|
+
return { code: "NO_MATCH", paths: { target, examples: [] } };
|
|
37
34
|
}
|
|
38
|
-
|
|
35
|
+
let updatedContent = content;
|
|
36
|
+
for (const { fullMatch, prefix, examplePath } of matches) {
|
|
37
|
+
const exampleContent = exampleMap.get(absolutePath(examplePath));
|
|
38
|
+
updatedContent = updatedContent.replaceAll(fullMatch, createReplacement({ content: exampleContent.content, examplePath, prefix }));
|
|
39
|
+
}
|
|
40
|
+
const paths = { examples: matches.map((m) => absolutePath(m.examplePath)), target };
|
|
41
|
+
return content === updatedContent
|
|
42
|
+
? { code: "NO_CHANGE", paths }
|
|
43
|
+
: { code: "UPDATE", paths, updatedContent };
|
|
39
44
|
}
|
|
45
|
+
/**
|
|
46
|
+
* A regex to match the embedex tag.
|
|
47
|
+
*
|
|
48
|
+
* Matching groups:
|
|
49
|
+
* 1. The block's prefix
|
|
50
|
+
* 2. The source file path
|
|
51
|
+
*/
|
|
52
|
+
const REGEX = /^(.*)<embedex source="(.+?)">\n[\S\s]*?<\/embedex>/gm;
|
|
40
53
|
function matchAll(params) {
|
|
41
54
|
const { content, exists } = params;
|
|
42
|
-
return [...content.matchAll(
|
|
55
|
+
return [...content.matchAll(REGEX)]
|
|
43
56
|
.map((match) => {
|
|
44
|
-
const [fullMatch, ,
|
|
57
|
+
const [fullMatch, prefix, examplePath] = match;
|
|
45
58
|
return isDefined(fullMatch) &&
|
|
46
|
-
isDefined(
|
|
47
|
-
isDefined(
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
? { fullMatch, language, indent, example }
|
|
59
|
+
isDefined(prefix) &&
|
|
60
|
+
isDefined(examplePath) &&
|
|
61
|
+
exists(examplePath)
|
|
62
|
+
? { fullMatch, prefix, examplePath }
|
|
51
63
|
: undefined;
|
|
52
64
|
})
|
|
53
65
|
.filter(isDefined);
|
|
54
66
|
}
|
|
55
|
-
function
|
|
56
|
-
const { content,
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
67
|
+
function createReplacement(params) {
|
|
68
|
+
const { content, examplePath, prefix } = params;
|
|
69
|
+
const contentHasCodeFence = content.includes("```");
|
|
70
|
+
const backticks = contentHasCodeFence ? "````" : "```";
|
|
71
|
+
const codeFenceId = CODE_FENCE_ID_BY_FILE_EXTENSION[(0, node_path_1.extname)(examplePath).slice(1)];
|
|
72
|
+
const escapedContent = content.replaceAll("*/", "*\\/").trimEnd().split("\n");
|
|
73
|
+
const lines = [
|
|
74
|
+
`<embedex source="${examplePath}">`,
|
|
75
|
+
"",
|
|
76
|
+
...(codeFenceId === ""
|
|
77
|
+
? escapedContent
|
|
78
|
+
: [`${backticks}${codeFenceId ?? ""}`, ...escapedContent, backticks]),
|
|
79
|
+
"",
|
|
80
|
+
"</embedex>",
|
|
81
|
+
];
|
|
82
|
+
return lines.map((line) => `${prefix}${line}`.trimEnd()).join("\n");
|
|
63
83
|
}
|
|
64
84
|
function isDefined(value) {
|
|
65
85
|
return value !== null && value !== undefined;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"processTargets.js","sourceRoot":"","sources":["../../../../../../packages/embedex/src/lib/internal/processTargets.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"processTargets.js","sourceRoot":"","sources":["../../../../../../packages/embedex/src/lib/internal/processTargets.ts"],"names":[],"mappings":";;AAkBA,wCAeC;AAjCD,yCAA0C;AAK1C,MAAM,+BAA+B,GAAqC;IACxE,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,EAAE,EAAE,IAAI;IACR,GAAG,EAAE,IAAI;IACT,EAAE,EAAE,EAAE;IACN,GAAG,EAAE,EAAE;IACP,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,EAAE,EAAE,IAAI;IACR,GAAG,EAAE,IAAI;CACD,CAAC;AAEX,SAAgB,cAAc,CAC5B,MAIE;IAEF,MAAM,EAAE,SAAS,EAAE,GAAG,IAAI,EAAE,GAAG,MAAM,CAAC;IAEtC,MAAM,MAAM,GAAY,EAAE,CAAC;IAC3B,KAAK,MAAM,KAAK,IAAI,SAAS,CAAC,OAAO,EAAE,EAAE,CAAC;QACxC,MAAM,CAAC,IAAI,CAAC,aAAa,CAAC,EAAE,GAAG,IAAI,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC;IACjD,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,SAAS,aAAa,CAAC,MAItB;IACC,MAAM,EAAE,GAAG,EAAE,UAAU,EAAE,KAAK,EAAE,GAAG,MAAM,CAAC;IAC1C,MAAM,CAAC,MAAM,EAAE,EAAE,OAAO,EAAE,QAAQ,EAAE,CAAC,GAAG,KAAK,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,EAAE,EAAE,GAAG,cAAc,EAAE,SAAS,CAAC,CAAC;QACvE,EAAE;QACF,YAAY;KACb,CAAC;IAEF,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,GAAG,MAAM,GAAG,IAAI,EAAE,CAAC,OAAO,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACtE,CAAC;AAED,SAAS,SAAS,CAAI,KAAoB;IACxC,OAAO,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS,CAAC;AAC/C,CAAC"}
|
|
@@ -4,9 +4,8 @@ export interface Example {
|
|
|
4
4
|
targets: TargetPath[];
|
|
5
5
|
}
|
|
6
6
|
export type ExampleMap = ReadonlyMap<ExamplePath, Example>;
|
|
7
|
-
interface Target {
|
|
7
|
+
export interface Target {
|
|
8
8
|
content: string;
|
|
9
9
|
examples: ReadonlySet<ExamplePath>;
|
|
10
10
|
}
|
|
11
11
|
export type TargetMap = ReadonlyMap<TargetPath, Target>;
|
|
12
|
-
export {};
|