texample
v0.0.6
Published
Execute your README markdown javascript examples
Downloads
120
Maintainers
Readme
Execute your README markdown javascript ESM examples
CLI
Arguments
- List of markdown files separated by comma (,)
- Optional markdown block index number, from 0
-g
run with globalThis as context
texample ./README.md,./docs/API.md
Example
Create a script that does the following:
import vm from 'node:vm';
import { createRequire } from 'node:module';
import { fileURLToPath } from 'node:url';
import { resolve as resolvePath } from 'node:path';
import { ExampleEvaluator } from 'texample';
if (!('SourceTextModule' in vm)) throw new Error('No SourceTextModule in vm, try using node --experimental-vm-modules flag');
const CWD = process.cwd();
const nodeRequire = createRequire(fileURLToPath(import.meta.url));
const packageDefinition = nodeRequire(resolvePath(CWD, 'package.json'));
const markdownFiles = process.argv[2] || './README.md';
const blockIdx = Number(process.argv[3]);
(async () => {
for (const filePath of markdownFiles.split(',')) {
await new ExampleEvaluator(filePath, packageDefinition, CWD, {
Buffer,
console,
setTimeout,
clearTimeout,
}).evaluate(blockIdx);
}
})();
Global context
Modules with side-effects should run with -g
flag since they probably change globalThis assignments.
texample ./test/docs/globals.md -g