@doccident/doccident 0.0.1
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 +24 -0
- package/README.md +179 -0
- package/bin/cmd.js +83 -0
- package/dist/doctest.js +158 -0
- package/dist/parse-code-snippets-from-markdown.js +86 -0
- package/package.json +64 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2015 Nick Johnstone
|
|
4
|
+
|
|
5
|
+
Copyright (c) 2025 Billaud Cipher
|
|
6
|
+
|
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
in the Software without restriction, including without limitation the rights
|
|
10
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
furnished to do so, subject to the following conditions:
|
|
13
|
+
|
|
14
|
+
The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
copies or substantial portions of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
SOFTWARE.
|
|
24
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
[](http://badge.fury.io/js/doccident)
|
|
2
|
+
[](https://travis-ci.org/Widdershin/doccident)
|
|
3
|
+
[](https://greenkeeper.io/)
|
|
4
|
+
|
|
5
|
+
* * *
|
|
6
|
+
|
|
7
|
+
# doccident
|
|
8
|
+
|
|
9
|
+
Test all the code in your markdown docs!
|
|
10
|
+
|
|
11
|
+
Why on earth?
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
As an open source developer, there are few things more embarrassing than a user opening an issue to inform you that your README example is broken! With `doccident`, you can rest easy knowing that your example code is *actually runnable*.
|
|
15
|
+
|
|
16
|
+
Installation
|
|
17
|
+
---
|
|
18
|
+
Just `npm install @doccident/doccident` and run `doccident`. It will run all of the Javascript code examples tucked away in your markdown, and let you know if any blow up.
|
|
19
|
+
|
|
20
|
+
Okay, how do I use it?
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
Let's try it on this repo!
|
|
24
|
+
|
|
25
|
+
```js
|
|
26
|
+
var a = 5;
|
|
27
|
+
|
|
28
|
+
var b = 10;
|
|
29
|
+
|
|
30
|
+
console.log(a + c);
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
There's a problem with that example. `doccident` finds it for us:
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
$ doccident
|
|
37
|
+
x..
|
|
38
|
+
|
|
39
|
+
Failed - README.md:32:17
|
|
40
|
+
evalmachine.<anonymous>:7
|
|
41
|
+
console.log(a + c);
|
|
42
|
+
^
|
|
43
|
+
|
|
44
|
+
ReferenceError: c is not defined
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Awesome! No excuse for broken documentation ever again, right? :wink:
|
|
48
|
+
|
|
49
|
+
We can also run specific files or folders by running `doccident` with a glob, like `doccident docs/**/*.md`. By default `doccident` will recursively run all the `.md` or `.markdown` files starting with the current directory, with the exception of the `node_modules` directory.
|
|
50
|
+
|
|
51
|
+
Note: `doccident` doesn't actually attempt to provide any guarantee that your code worked, only that it didn't explode in a horrible fashion. If you would like to use `doccident` for actually testing the correctness of your code, you can add some `assert`s to your examples.
|
|
52
|
+
|
|
53
|
+
`doccident` is not a replacement for your test suite. It's designed to run with your CI build and give you peace of mind that all of your examples are at least vaguely runnable.
|
|
54
|
+
|
|
55
|
+
So how do I write those examples?
|
|
56
|
+
---
|
|
57
|
+
|
|
58
|
+
In your markdown files, anything inside of code blocks with 'js' or 'es6' will be run. E.g:
|
|
59
|
+
|
|
60
|
+
```js
|
|
61
|
+
console.log("Yay, tests in my docs");
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
```es6
|
|
65
|
+
const a = 5;
|
|
66
|
+
console.log({a, foo: 'test'});
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
I have a code example I don't want tested!
|
|
70
|
+
---
|
|
71
|
+
You can tell `doccident` to skip examples by adding `<!-- skip-example -->` before the example. E.g:
|
|
72
|
+
|
|
73
|
+
<!-- skip-example -->
|
|
74
|
+
```js
|
|
75
|
+
// not a runnable example
|
|
76
|
+
|
|
77
|
+
var foo = download(...);
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
How do requires work? And other setup logic?
|
|
81
|
+
---
|
|
82
|
+
|
|
83
|
+
You can `require` any needed modules or example helpers in `.doccident-setup.js`. E.g:
|
|
84
|
+
|
|
85
|
+
<!-- skip-example -->
|
|
86
|
+
```js
|
|
87
|
+
// .doccident-setup.js
|
|
88
|
+
module.exports = {
|
|
89
|
+
require: {
|
|
90
|
+
Rx: require('rx')
|
|
91
|
+
},
|
|
92
|
+
|
|
93
|
+
globals: {
|
|
94
|
+
$: require('jquery')
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Anything exported under `require` will then be used by any examples that `require` that key.
|
|
100
|
+
You must explicitly configure all of the dependencies used in your examples.
|
|
101
|
+
|
|
102
|
+
Anything exported under `globals` will be available globally across all examples.
|
|
103
|
+
|
|
104
|
+
You can also specify a regexRequire section to handle anything more complex than an exact string match!
|
|
105
|
+
|
|
106
|
+
<!-- skip-example -->
|
|
107
|
+
```js
|
|
108
|
+
// .doccident-setup.js
|
|
109
|
+
module.exports = {
|
|
110
|
+
require: {
|
|
111
|
+
Rx: require('rx')
|
|
112
|
+
},
|
|
113
|
+
|
|
114
|
+
regexRequire: {
|
|
115
|
+
'rx/(.*)': function (fullPath, matchedModuleName) {
|
|
116
|
+
return require('./dist/' + matchedModuleName);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
Do I have to enable es6 support?
|
|
123
|
+
---
|
|
124
|
+
|
|
125
|
+
Nope, ES6 support is on by default. You can disable `babel` support
|
|
126
|
+
in your `.doccident-setup.js` file.
|
|
127
|
+
This will speed things up drastically:
|
|
128
|
+
|
|
129
|
+
<!-- skip-example -->
|
|
130
|
+
```js
|
|
131
|
+
//.doccident-setup.js
|
|
132
|
+
module.exports = {
|
|
133
|
+
babel: false
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
What if I have global state that needs to be reset after my examples run?
|
|
138
|
+
---
|
|
139
|
+
<!-- skip-example -->
|
|
140
|
+
```js
|
|
141
|
+
//.doccident-setup.js
|
|
142
|
+
module.exports = {
|
|
143
|
+
beforeEach: function () {
|
|
144
|
+
// reset your awesome global state
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
You can specify a function to be run before each example in your `.doccident-setup.js`.
|
|
150
|
+
|
|
151
|
+
What if I want to remove custom syntax from examples before processing?
|
|
152
|
+
---
|
|
153
|
+
|
|
154
|
+
<!-- skip-example -->
|
|
155
|
+
```js
|
|
156
|
+
//.doccident-setup.js
|
|
157
|
+
module.exports = {
|
|
158
|
+
transformCode(code) {
|
|
159
|
+
// Remove ... from code syntax
|
|
160
|
+
return code.replace(/\.\.\./g, "");
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
Who uses doccident?
|
|
166
|
+
---
|
|
167
|
+
|
|
168
|
+
All of these projects either run `doccident` with `npm test` or as part of their CI process:
|
|
169
|
+
|
|
170
|
+
* [lodash](https://github.com/lodash/lodash)
|
|
171
|
+
* [Moment](https://github.com/moment/momentjs.com)
|
|
172
|
+
* [RxJS](https://github.com/ReactiveX/RxJS)
|
|
173
|
+
* [most](https://github.com/cujojs/most)
|
|
174
|
+
* [xstream](https://github.com/staltz/xstream)
|
|
175
|
+
* [cyclejs/time](https://github.com/cyclejs/time)
|
|
176
|
+
* [rx.schedulers](https://github.com/Reactive-Extensions/rx.schedulers)
|
|
177
|
+
* [rx.priorityqueue](https://github.com/Reactive-Extensions/rx.priorityqueue)
|
|
178
|
+
* [rx.disposables](https://github.com/Reactive-Extensions/rx.disposables)
|
|
179
|
+
* [rx-undoable](https://github.com/Widdershin/rx-undoable)
|
package/bin/cmd.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
#! /usr/bin/env node
|
|
2
|
+
/* eslint-disable no-console */
|
|
3
|
+
/* eslint-disable @typescript-eslint/no-require-imports */
|
|
4
|
+
'use strict';
|
|
5
|
+
|
|
6
|
+
const { version } = require('../package');
|
|
7
|
+
const doctest = require('..');
|
|
8
|
+
const program = require('commander');
|
|
9
|
+
const path = require('path');
|
|
10
|
+
const glob = require('glob');
|
|
11
|
+
const fs = require('fs');
|
|
12
|
+
const { pathToFileURL } = require('url');
|
|
13
|
+
|
|
14
|
+
const DEFAULT_GLOB = '**/*.+(md|markdown)';
|
|
15
|
+
const DEFAULT_IGNORE = [
|
|
16
|
+
'**/node_modules/**',
|
|
17
|
+
'**/bower_components/**'
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
// Config
|
|
21
|
+
const config = {
|
|
22
|
+
require: {},
|
|
23
|
+
globals: {},
|
|
24
|
+
ignore: [],
|
|
25
|
+
testOutput: false
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
// Setup commander
|
|
29
|
+
program
|
|
30
|
+
.name('doccident')
|
|
31
|
+
.description('Test all the code in your markdown docs!')
|
|
32
|
+
.version(version, '-v, --version', 'output the current version')
|
|
33
|
+
.helpOption('-h, --help', 'output usage informations')
|
|
34
|
+
.option('-c, --config <path>', 'custom config location', path.join(process.cwd(), '/.doccident-setup.js'))
|
|
35
|
+
.option('--test-output', 'output the test results to the console')
|
|
36
|
+
.parse(process.argv);
|
|
37
|
+
|
|
38
|
+
// Parse config file
|
|
39
|
+
(async () => {
|
|
40
|
+
if (program.config) {
|
|
41
|
+
const configPath = path.resolve(program.config);
|
|
42
|
+
|
|
43
|
+
if (fs.existsSync(configPath)) {
|
|
44
|
+
try {
|
|
45
|
+
const customConfig = await import(pathToFileURL(configPath).href);
|
|
46
|
+
Object.assign(config, customConfig.default || customConfig);
|
|
47
|
+
} catch (e) {
|
|
48
|
+
console.error(`Cannot load config "${configPath}":`, e);
|
|
49
|
+
process.exit(1);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (program.testOutput) {
|
|
55
|
+
config.testOutput = true;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Resolve files
|
|
59
|
+
glob(
|
|
60
|
+
program.args[0] || DEFAULT_GLOB,
|
|
61
|
+
{
|
|
62
|
+
ignore: [...config.ignore, ...DEFAULT_IGNORE]
|
|
63
|
+
},
|
|
64
|
+
(err, files) => {
|
|
65
|
+
|
|
66
|
+
if (err) {
|
|
67
|
+
console.trace(err);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Run tests
|
|
71
|
+
const results = doctest.runTests(files, config);
|
|
72
|
+
|
|
73
|
+
console.log('\n');
|
|
74
|
+
doctest.printResults(results);
|
|
75
|
+
|
|
76
|
+
// Exit with error-code if any test failed
|
|
77
|
+
const failures = results.filter(result => result.status === 'fail');
|
|
78
|
+
if (failures.length > 0) {
|
|
79
|
+
process.exit(1);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
);
|
|
83
|
+
})();
|
package/dist/doctest.js
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/* eslint-disable no-console */
|
|
2
|
+
"use strict";
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.runTests = runTests;
|
|
5
|
+
exports.printResults = printResults;
|
|
6
|
+
var fs_1 = require("fs");
|
|
7
|
+
var vm_1 = require("vm");
|
|
8
|
+
var core_1 = require("@babel/core");
|
|
9
|
+
var preset_env_1 = require("@babel/preset-env");
|
|
10
|
+
var chalk_1 = require("chalk");
|
|
11
|
+
function flatten(arr) {
|
|
12
|
+
return Array.prototype.concat.apply([], arr);
|
|
13
|
+
}
|
|
14
|
+
var parse_code_snippets_from_markdown_1 = require("./parse-code-snippets-from-markdown");
|
|
15
|
+
function runTests(files, config) {
|
|
16
|
+
var results = files
|
|
17
|
+
.map(read)
|
|
18
|
+
.map(parse_code_snippets_from_markdown_1.default)
|
|
19
|
+
.map(testFile(config));
|
|
20
|
+
return flatten(results);
|
|
21
|
+
}
|
|
22
|
+
function read(fileName) {
|
|
23
|
+
return { contents: (0, fs_1.readFileSync)(fileName, "utf8"), fileName: fileName };
|
|
24
|
+
}
|
|
25
|
+
function makeTestSandbox(config) {
|
|
26
|
+
function sandboxRequire(moduleName) {
|
|
27
|
+
for (var regexRequire in config.regexRequire) {
|
|
28
|
+
var regex = new RegExp(regexRequire);
|
|
29
|
+
var match = regex.exec(moduleName);
|
|
30
|
+
var handler = config.regexRequire[regexRequire];
|
|
31
|
+
if (match) {
|
|
32
|
+
return handler.apply(void 0, match);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
if (config.require[moduleName] === undefined) {
|
|
36
|
+
throw moduleNotFoundError(moduleName);
|
|
37
|
+
}
|
|
38
|
+
return config.require[moduleName];
|
|
39
|
+
}
|
|
40
|
+
var sandboxConsole = {
|
|
41
|
+
log: function () { return null; },
|
|
42
|
+
};
|
|
43
|
+
var sandboxGlobals = { require: sandboxRequire, console: config.testOutput ? console : sandboxConsole };
|
|
44
|
+
var sandbox = Object.assign({}, sandboxGlobals, config.globals);
|
|
45
|
+
return sandbox;
|
|
46
|
+
}
|
|
47
|
+
function testFile(config) {
|
|
48
|
+
return function testFileWithConfig(args) {
|
|
49
|
+
var codeSnippets = args.codeSnippets;
|
|
50
|
+
var fileName = args.fileName;
|
|
51
|
+
var shareCodeInFile = args.shareCodeInFile;
|
|
52
|
+
var results;
|
|
53
|
+
if (shareCodeInFile) {
|
|
54
|
+
var sandbox = makeTestSandbox(config);
|
|
55
|
+
results = codeSnippets.map(test(config, fileName, sandbox));
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
results = codeSnippets.map(test(config, fileName));
|
|
59
|
+
}
|
|
60
|
+
return results;
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
function test(config, filename, sandbox) {
|
|
64
|
+
return function (codeSnippet) {
|
|
65
|
+
if (codeSnippet.skip) {
|
|
66
|
+
return { status: "skip", codeSnippet: codeSnippet, stack: "" };
|
|
67
|
+
}
|
|
68
|
+
var success = false;
|
|
69
|
+
var stack = "";
|
|
70
|
+
var code = codeSnippet.code;
|
|
71
|
+
if (config.transformCode) {
|
|
72
|
+
try {
|
|
73
|
+
code = config.transformCode(code);
|
|
74
|
+
}
|
|
75
|
+
catch (e) {
|
|
76
|
+
return { status: "fail", codeSnippet: codeSnippet, stack: "Encountered an error while transforming snippet: \n" + e.stack };
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
var perSnippetSandbox;
|
|
80
|
+
if (sandbox === undefined) {
|
|
81
|
+
perSnippetSandbox = makeTestSandbox(config);
|
|
82
|
+
}
|
|
83
|
+
if (config.beforeEach) {
|
|
84
|
+
config.beforeEach();
|
|
85
|
+
}
|
|
86
|
+
var options = {
|
|
87
|
+
presets: [preset_env_1.default],
|
|
88
|
+
};
|
|
89
|
+
try {
|
|
90
|
+
if (config.babel !== false) {
|
|
91
|
+
code = (0, core_1.transformSync)(code, options).code;
|
|
92
|
+
}
|
|
93
|
+
(0, vm_1.runInNewContext)(code, perSnippetSandbox || sandbox);
|
|
94
|
+
success = true;
|
|
95
|
+
}
|
|
96
|
+
catch (e) {
|
|
97
|
+
stack = e.stack || "";
|
|
98
|
+
}
|
|
99
|
+
var status = success ? "pass" : "fail";
|
|
100
|
+
process.stdout.write(success ? chalk_1.default.green(".") : chalk_1.default.red("x"));
|
|
101
|
+
return { status: status, codeSnippet: codeSnippet, stack: stack };
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
function printResults(results) {
|
|
105
|
+
results.filter(function (result) { return result.status === "fail"; }).forEach(printFailure);
|
|
106
|
+
var passingCount = results.filter(function (result) { return result.status === "pass"; })
|
|
107
|
+
.length;
|
|
108
|
+
var failingCount = results.filter(function (result) { return result.status === "fail"; })
|
|
109
|
+
.length;
|
|
110
|
+
var skippingCount = results.filter(function (result) { return result.status === "skip"; })
|
|
111
|
+
.length;
|
|
112
|
+
function successfulRun() {
|
|
113
|
+
return failingCount === 0;
|
|
114
|
+
}
|
|
115
|
+
console.log(chalk_1.default.green("Passed: " + passingCount));
|
|
116
|
+
if (skippingCount > 0) {
|
|
117
|
+
console.log(chalk_1.default.yellow("Skipped: " + skippingCount));
|
|
118
|
+
}
|
|
119
|
+
if (successfulRun()) {
|
|
120
|
+
console.log(chalk_1.default.green("\nSuccess!"));
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
console.log(chalk_1.default.red("Failed: " + failingCount));
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
function printFailure(result) {
|
|
127
|
+
console.log(chalk_1.default.red("Failed - ".concat(markDownErrorLocation(result))));
|
|
128
|
+
var stackDetails = relevantStackDetails(result.stack);
|
|
129
|
+
console.log(stackDetails);
|
|
130
|
+
var variableNotDefined = stackDetails.match(/(\w+) is not defined/);
|
|
131
|
+
if (variableNotDefined) {
|
|
132
|
+
var variableName = variableNotDefined[1];
|
|
133
|
+
console.log("You can declare ".concat(chalk_1.default.blue(variableName), " in the ").concat(chalk_1.default.blue("globals"), " section in ").concat(chalk_1.default.grey(".doccident-setup.js")));
|
|
134
|
+
console.log("\nFor example:\n".concat(chalk_1.default.grey("// .doccident-setup.js"), "\nmodule.exports = {\n globals: {\n ").concat(chalk_1.default.blue(variableName), ": ...\n }\n}\n "));
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
function relevantStackDetails(stack) {
|
|
138
|
+
var match = stack.match(/([\w\W]*?)at eval/) ||
|
|
139
|
+
// eslint-disable-next-line no-useless-escape
|
|
140
|
+
stack.match(/([\w\W]*)at [\w*\/]*?doctest.js/);
|
|
141
|
+
if (match !== null) {
|
|
142
|
+
return match[1];
|
|
143
|
+
}
|
|
144
|
+
return stack;
|
|
145
|
+
}
|
|
146
|
+
function moduleNotFoundError(moduleName) {
|
|
147
|
+
return new Error("\nAttempted to require '".concat(chalk_1.default.blue(moduleName), "' but was not found in config.\nYou need to include it in the require section of your ").concat(chalk_1.default.grey(".doccident-setup.js"), " file.\n\nFor example:\n").concat(chalk_1.default.grey("// .doccident-setup.js"), "\nmodule.exports = {\n require: {\n ").concat(chalk_1.default.blue("'".concat(moduleName, "': require('").concat(moduleName, "')")), "\n }\n}\n "));
|
|
148
|
+
}
|
|
149
|
+
function markDownErrorLocation(result) {
|
|
150
|
+
var match = result.stack.match(/eval.*<.*>:(\d+):(\d+)/);
|
|
151
|
+
if (match) {
|
|
152
|
+
var mdLineNumber = parseInt(match[1], 10);
|
|
153
|
+
var columnNumber = parseInt(match[2], 10);
|
|
154
|
+
var lineNumber = result.codeSnippet.lineNumber + mdLineNumber;
|
|
155
|
+
return "".concat(result.codeSnippet.fileName, ":").concat(lineNumber, ":").concat(columnNumber);
|
|
156
|
+
}
|
|
157
|
+
return "".concat(result.codeSnippet.fileName, ":").concat(result.codeSnippet.lineNumber);
|
|
158
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
var isStartOfSnippet = function (line) {
|
|
4
|
+
return line.trim().match(/```\W*(JavaScript|js|es6)\s?$/i);
|
|
5
|
+
};
|
|
6
|
+
var isEndOfSnippet = function (line) { return line.trim() === "```"; };
|
|
7
|
+
var isSkip = function (line) { return line.trim() === "<!-- skip-example -->"; };
|
|
8
|
+
var isCodeSharedInFile = function (line) {
|
|
9
|
+
return line.trim() === "<!-- share-code-between-examples -->";
|
|
10
|
+
};
|
|
11
|
+
function startNewSnippet(snippets, fileName, lineNumber) {
|
|
12
|
+
var skip = snippets.skip;
|
|
13
|
+
snippets.skip = false;
|
|
14
|
+
return Object.assign(snippets, {
|
|
15
|
+
snippets: snippets.snippets.concat([
|
|
16
|
+
{ code: "", fileName: fileName, lineNumber: lineNumber, complete: false, skip: skip }
|
|
17
|
+
])
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
function addLineToLastSnippet(line) {
|
|
21
|
+
return function addLine(snippets) {
|
|
22
|
+
var lastSnippet = snippets.snippets[snippets.snippets.length - 1];
|
|
23
|
+
if (lastSnippet && !lastSnippet.complete) {
|
|
24
|
+
lastSnippet.code += line + "\n";
|
|
25
|
+
}
|
|
26
|
+
return snippets;
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
function endSnippet(snippets, fileName, lineNumber) {
|
|
30
|
+
// eslint-disable-next-line no-console
|
|
31
|
+
console.log("endSnippet", snippets, fileName, lineNumber);
|
|
32
|
+
var lastSnippet = snippets.snippets[snippets.snippets.length - 1];
|
|
33
|
+
if (lastSnippet) {
|
|
34
|
+
lastSnippet.complete = true;
|
|
35
|
+
}
|
|
36
|
+
return snippets;
|
|
37
|
+
}
|
|
38
|
+
function skip(snippets) {
|
|
39
|
+
snippets.skip = true;
|
|
40
|
+
return snippets;
|
|
41
|
+
}
|
|
42
|
+
function shareCodeInFile(snippets) {
|
|
43
|
+
snippets.shareCodeInFile = true;
|
|
44
|
+
return snippets;
|
|
45
|
+
}
|
|
46
|
+
function parseLine(line) {
|
|
47
|
+
if (isStartOfSnippet(line)) {
|
|
48
|
+
return startNewSnippet;
|
|
49
|
+
}
|
|
50
|
+
if (isEndOfSnippet(line)) {
|
|
51
|
+
return endSnippet;
|
|
52
|
+
}
|
|
53
|
+
if (isSkip(line)) {
|
|
54
|
+
return skip;
|
|
55
|
+
}
|
|
56
|
+
if (isCodeSharedInFile(line)) {
|
|
57
|
+
return shareCodeInFile;
|
|
58
|
+
}
|
|
59
|
+
return addLineToLastSnippet(line);
|
|
60
|
+
}
|
|
61
|
+
function parseCodeSnippets(args) {
|
|
62
|
+
var contents = args.contents;
|
|
63
|
+
var fileName = args.fileName;
|
|
64
|
+
var initialState = {
|
|
65
|
+
snippets: [],
|
|
66
|
+
shareCodeInFile: false,
|
|
67
|
+
complete: false
|
|
68
|
+
};
|
|
69
|
+
var results = contents
|
|
70
|
+
.split("\n")
|
|
71
|
+
.map(parseLine)
|
|
72
|
+
.reduce(function (snippets, lineAction, index) {
|
|
73
|
+
return lineAction(snippets, fileName, index + 1);
|
|
74
|
+
}, initialState);
|
|
75
|
+
var codeSnippets = results.snippets;
|
|
76
|
+
var lastSnippet = codeSnippets[codeSnippets.length - 1];
|
|
77
|
+
if (lastSnippet && !lastSnippet.complete) {
|
|
78
|
+
throw new Error("Snippet parsing was incomplete");
|
|
79
|
+
}
|
|
80
|
+
return {
|
|
81
|
+
fileName: fileName,
|
|
82
|
+
codeSnippets: codeSnippets,
|
|
83
|
+
shareCodeInFile: results.shareCodeInFile
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
exports.default = parseCodeSnippets;
|
package/package.json
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@doccident/doccident",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Test all the code in your markdown docs!",
|
|
5
|
+
"main": "dist/doctest.js",
|
|
6
|
+
"files": [
|
|
7
|
+
"dist",
|
|
8
|
+
"bin"
|
|
9
|
+
],
|
|
10
|
+
"bin": {
|
|
11
|
+
"doccident": "bin/cmd.js"
|
|
12
|
+
},
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "https://github.com/BillaudCipher/doccident"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"markdown",
|
|
19
|
+
"test",
|
|
20
|
+
"documentation"
|
|
21
|
+
],
|
|
22
|
+
"contributors": [
|
|
23
|
+
"Nick Johnstone",
|
|
24
|
+
"Billaud Cipher <BillaudCipher@proton.me>"
|
|
25
|
+
],
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"bugs": {
|
|
28
|
+
"url": "https://github.com/BillaudCipher/doccident/issues"
|
|
29
|
+
},
|
|
30
|
+
"homepage": "https://github.com/BillaudCipher/doccident",
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@babel/preset-typescript": "^7.27.1",
|
|
33
|
+
"@eslint/eslintrc": "^3.3.1",
|
|
34
|
+
"@eslint/js": "^9.23.0",
|
|
35
|
+
"@types/node": "^22.15.17",
|
|
36
|
+
"@typescript-eslint/eslint-plugin": "^8.32.0",
|
|
37
|
+
"@typescript-eslint/parser": "^8.32.0",
|
|
38
|
+
"eslint": "^9.24.0",
|
|
39
|
+
"eslint-plugin-import": "^2.31.0",
|
|
40
|
+
"jest": "^29.7.0",
|
|
41
|
+
"typescript": "^5.8.3"
|
|
42
|
+
},
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"@babel/core": "^7.27.1",
|
|
45
|
+
"@babel/preset-env": "^7.27.2",
|
|
46
|
+
"chalk": "^2.4.2",
|
|
47
|
+
"commander": "^4.1.1",
|
|
48
|
+
"glob": "^7.2.3",
|
|
49
|
+
"globals": "^16.1.0"
|
|
50
|
+
},
|
|
51
|
+
"babel": {
|
|
52
|
+
"presets": [
|
|
53
|
+
"@babel/preset-env",
|
|
54
|
+
"@babel/preset-typescript"
|
|
55
|
+
]
|
|
56
|
+
},
|
|
57
|
+
"scripts": {
|
|
58
|
+
"build": "tsc",
|
|
59
|
+
"lint": "eslint . --ext .ts",
|
|
60
|
+
"test": "jest",
|
|
61
|
+
"prepublish": "pnpm run build",
|
|
62
|
+
"clean": "rm -rf dist"
|
|
63
|
+
}
|
|
64
|
+
}
|