|
| 1 | +import {remark} from 'remark' |
| 2 | +import test from 'tape' |
| 3 | +import {engine as engineCb} from 'unified-engine' |
| 4 | +import {promisify} from 'node:util' |
| 5 | +import {VFile} from 'vfile' |
| 6 | +import remarkValidateLinks from '../index.js' |
| 7 | + |
| 8 | +const engine = promisify(engineCb) |
| 9 | + |
| 10 | +// Should ignore links to other branches. |
| 11 | +const options = {repository: 'https://github.com/wooorm/test.git#main'} |
| 12 | + |
| 13 | +test('same branch, working link', async (t) => { |
| 14 | + t.deepEqual( |
| 15 | + await process( |
| 16 | + '[](https://github.com/wooorm/test/blob/main/examples/github.md#hello)' |
| 17 | + ), |
| 18 | + [], |
| 19 | + 'nothing to report' |
| 20 | + ) |
| 21 | +}) |
| 22 | + |
| 23 | +test('other branch, working link', async (t) => { |
| 24 | + t.deepEqual( |
| 25 | + await process( |
| 26 | + '[](https://github.com/wooorm/test/blob/foo-bar/examples/github.md#hello)' |
| 27 | + ), |
| 28 | + [], |
| 29 | + 'nothing to ignore' |
| 30 | + ) |
| 31 | +}) |
| 32 | + |
| 33 | +test('same branch, no such heading', async (t) => { |
| 34 | + t.deepEqual( |
| 35 | + await process( |
| 36 | + '[](https://github.com/wooorm/test/blob/main/examples/github.md#world)' |
| 37 | + ), |
| 38 | + [ |
| 39 | + 'input.md:1:1-1:70: Link to unknown heading in `examples/github.md`: `world`' |
| 40 | + ], |
| 41 | + 'should be reported' |
| 42 | + ) |
| 43 | +}) |
| 44 | + |
| 45 | +test('other branch, no such heading', async (t) => { |
| 46 | + t.deepEqual( |
| 47 | + await process( |
| 48 | + '[](https://github.com/wooorm/test/blob/foo-bar/examples/github.md#world)' |
| 49 | + ), |
| 50 | + [], |
| 51 | + 'should be ignored' |
| 52 | + ) |
| 53 | +}) |
| 54 | + |
| 55 | +test('same branch, no such file', async (t) => { |
| 56 | + t.deepEqual( |
| 57 | + await process( |
| 58 | + '[](https://github.com/wooorm/test/blob/main/examples/world.md#hello)' |
| 59 | + ), |
| 60 | + [ |
| 61 | + 'input.md:1:1-1:69: Link to unknown file: `examples/world.md`', |
| 62 | + 'input.md:1:1-1:69: Link to unknown heading in `examples/world.md`: `hello`' |
| 63 | + ], |
| 64 | + 'should be reported' |
| 65 | + ) |
| 66 | +}) |
| 67 | + |
| 68 | +test('other branch, no such file', async (t) => { |
| 69 | + t.deepEqual( |
| 70 | + await process( |
| 71 | + '[](https://github.com/wooorm/test/blob/foo-bar/examples/world.md#hello)' |
| 72 | + ), |
| 73 | + [], |
| 74 | + 'should be ignored' |
| 75 | + ) |
| 76 | +}) |
| 77 | + |
| 78 | +/** |
| 79 | + * Run the plugin on a markdown test case and return a list of stringified messages. |
| 80 | + * unified-engine is needed to cross-reference links between files, by creating a FileSet. |
| 81 | + * |
| 82 | + * @param {string} value |
| 83 | + */ |
| 84 | +async function process(value) { |
| 85 | + const file = new VFile({value, path: 'input.md'}) |
| 86 | + await engine({ |
| 87 | + processor: remark, |
| 88 | + files: [file], |
| 89 | + plugins: [[remarkValidateLinks, options]], |
| 90 | + silent: true |
| 91 | + }) |
| 92 | + return file.messages.map((message) => String(message)) |
| 93 | +} |
0 commit comments