|
| 1 | +import assert from 'node:assert'; |
| 2 | +import path from 'node:path'; |
| 3 | +import { describe, it } from 'node:test'; |
| 4 | +import fs from 'fs-extra'; |
| 5 | +import { glob } from 'glob'; |
| 6 | +import remark from 'remark'; |
| 7 | +import github from 'remark-github'; |
| 8 | + |
| 9 | +const root = path.resolve('tmp/docs'); |
| 10 | + |
| 11 | +/** |
| 12 | + * @param {any} node |
| 13 | + * @param {Set<string>} files |
| 14 | + * @param {string} file |
| 15 | + */ |
| 16 | +function checkNode(node, files, file) { |
| 17 | + if (node.type === 'link') { |
| 18 | + /** @type {import('mdast').Link} */ |
| 19 | + const link = node; |
| 20 | + assert.ok( |
| 21 | + !link.url.startsWith('/'), |
| 22 | + `Link should be external or relative: ${link.url}`, |
| 23 | + ); |
| 24 | + |
| 25 | + if (link.url.startsWith('.') && !/^https?:\/\//.test(link.url)) { |
| 26 | + // absolute path |
| 27 | + const absPath = path.resolve( |
| 28 | + 'tmp/docs', |
| 29 | + path.dirname(file), |
| 30 | + link.url.replace(/#.*/, ''), |
| 31 | + ); |
| 32 | + // relative path |
| 33 | + const relPath = absPath.substring(root.length + 1); |
| 34 | + |
| 35 | + assert.ok( |
| 36 | + files.has(relPath), |
| 37 | + `File not found: ${link.url} in ${file} -> ${relPath}`, |
| 38 | + ); |
| 39 | + } else { |
| 40 | + assert.ok( |
| 41 | + !link.url.startsWith('https://docs.renovatebot.com/'), |
| 42 | + `Docs links should be relative: ${link.url}`, |
| 43 | + ); |
| 44 | + } |
| 45 | + } else if ('children' in node) { |
| 46 | + for (const child of node.children) { |
| 47 | + checkNode(child, files, file); |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +describe('index', async () => { |
| 53 | + await describe('validate links', async () => { |
| 54 | + const todo = await glob('**/*.md', { cwd: 'tmp/docs' }); |
| 55 | + const files = new Set(todo); |
| 56 | + |
| 57 | + // Files from https://github.com/renovatebot/renovatebot.github.io/tree/main/src |
| 58 | + files.add('index.md'); |
| 59 | + |
| 60 | + let c = 0; |
| 61 | + |
| 62 | + for (const file of todo) { |
| 63 | + c++; |
| 64 | + |
| 65 | + await it(`${file}`, async () => { |
| 66 | + const node = remark() |
| 67 | + .use(github) |
| 68 | + .parse(await fs.readFile(`tmp/docs/${file}`, 'utf8')); |
| 69 | + checkNode(node, files, file); |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + assert.ok(c > 0, 'Should find at least one file'); |
| 74 | + }); |
| 75 | +}); |
0 commit comments