Skip to content

Commit 759b3b7

Browse files
committed
feat: Add ability to exclude parts of an example with ignore-extract and end-ignore
1 parent 7c02d3a commit 759b3b7

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

README.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ function sumArray(arr: number[]): number {
5151
```
5252
````
5353

54+
By using `example-extractor`, you can easily maintain and manage code examples for documentation purposes, ensuring that your examples are always up-to-date with your source code.
55+
5456
### Example Usage in Source Files
5557

5658
You can have multiple examples in your source files. Each example should have a unique name:
@@ -69,7 +71,39 @@ function maxArray(arr: number[]): number {
6971
// end-example
7072
```
7173

72-
By using `example-extractor`, you can easily maintain and manage code examples for documentation purposes, ensuring that your examples are always up-to-date with your source code.
74+
### Ignoring lines
75+
76+
Sometimes you want to exclude some code that needs to be there for your example to compile.
77+
If you'd like to exclude some lines in your examples, you can turn off extraction during an example with `ignore-extract` and `end-ignore`:
78+
79+
```typescript
80+
// example-extract general
81+
function reduceNumbers(arr: number[]): number {
82+
return arr.reduce(
83+
/* your function goes here */
84+
// ignore-example
85+
(sum, current) => {
86+
return sum + current;
87+
},
88+
// end-ignore
89+
0,
90+
);
91+
}
92+
// end-example
93+
```
94+
95+
This would result in the following extraction:
96+
97+
````markdown
98+
```ts
99+
function reduceNumbers(arr: number[]): number {
100+
return arr.reduce(
101+
/* your function goes here */
102+
0,
103+
);
104+
}
105+
```
106+
````
73107

74108
### Clearing the examples directory
75109

src/domain/exampleExtractor.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@ import { Example, ExampleLocation } from './types.js';
33

44
const START_REGEX = /^\s*(\/\/|\/\*|\*|\*\/)\s*example-extract\s*(\w+.\w+)?/;
55
const END_REGEX = /^\s*(\/\/|\/\*|\*|\*\/)\s*end-example/;
6+
const START_IGNORE_REGEX = /^\s*(\/\/|\/\*|\*|\*\/)\s*ignore-extract/;
7+
const END_IGNORE_REGEX = /^\s*(\/\/|\/\*|\*|\*\/)\s*end-ignore/;
68

79
// example-extract partial-example
810
interface PartialExample {
11+
// The name of this example
912
name: string;
13+
// The lines of this example
1014
lines: string[];
15+
// Whether we're currently ignoring lines
16+
ignoring: boolean;
1117
}
1218
// end-example
1319

@@ -34,6 +40,7 @@ export const extractExampleSections = (
3440
partialExample = {
3541
name: startMatch[2] || `${context.basename}-${count}`,
3642
lines: [],
43+
ignoring: false,
3744
};
3845
count += 1;
3946
} else if (partialExample !== undefined) {
@@ -44,6 +51,14 @@ export const extractExampleSections = (
4451
context,
4552
});
4653
partialExample = undefined;
54+
return;
55+
}
56+
if (partialExample.ignoring) {
57+
if (END_IGNORE_REGEX.exec(line)) {
58+
partialExample.ignoring = false;
59+
}
60+
} else if (START_IGNORE_REGEX.exec(line)) {
61+
partialExample.ignoring = true;
4762
} else {
4863
partialExample.lines.push(line);
4964
}

0 commit comments

Comments
 (0)