|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + */ |
| 4 | + |
| 5 | +// To do: Make this ESM. |
| 6 | +// To do: properly check heading numbers (headings with the same text get |
| 7 | +// numbered, this script doesn’t check that). |
| 8 | + |
| 9 | +const assert = require('assert'); |
| 10 | +const fs = require('fs'); |
| 11 | +const GithubSlugger = require('github-slugger'); |
| 12 | +const walk = require('./walk'); |
| 13 | + |
| 14 | +let modules; |
| 15 | + |
| 16 | +function stripLinks(line) { |
| 17 | + return line.replace(/\[([^\]]+)\]\([^)]+\)/, (match, p1) => p1); |
| 18 | +} |
| 19 | + |
| 20 | +function addHeaderID(line, slugger) { |
| 21 | + // check if we're a header at all |
| 22 | + if (!line.startsWith('#')) { |
| 23 | + return line; |
| 24 | + } |
| 25 | + |
| 26 | + const match = |
| 27 | + /^(#+\s+)(.+?)(\s*\{(?:\/\*|#)([^\}\*\/]+)(?:\*\/)?\}\s*)?$/.exec(line); |
| 28 | + const before = match[1] + match[2]; |
| 29 | + const proc = modules |
| 30 | + .unified() |
| 31 | + .use(modules.remarkParse) |
| 32 | + .use(modules.remarkSlug); |
| 33 | + const tree = proc.runSync(proc.parse(before)); |
| 34 | + const head = tree.children[0]; |
| 35 | + assert( |
| 36 | + head && head.type === 'heading', |
| 37 | + 'expected `' + |
| 38 | + before + |
| 39 | + '` to be a heading, is it using a normal space after `#`?' |
| 40 | + ); |
| 41 | + const autoId = head.data.id; |
| 42 | + const existingId = match[4]; |
| 43 | + const id = existingId || autoId; |
| 44 | + // Ignore numbers: |
| 45 | + const cleanExisting = existingId |
| 46 | + ? existingId.replace(/-\d+$/, '') |
| 47 | + : undefined; |
| 48 | + const cleanAuto = autoId.replace(/-\d+$/, ''); |
| 49 | + |
| 50 | + if (cleanExisting && cleanExisting !== cleanAuto) { |
| 51 | + console.log( |
| 52 | + 'Note: heading `%s` has a different ID (`%s`) than what GH generates for it: `%s`:', |
| 53 | + before, |
| 54 | + existingId, |
| 55 | + autoId |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + return match[1] + match[2] + ' {/*' + id + '*/}'; |
| 60 | +} |
| 61 | + |
| 62 | +function addHeaderIDs(lines) { |
| 63 | + // Sluggers should be per file |
| 64 | + const slugger = new GithubSlugger(); |
| 65 | + let inCode = false; |
| 66 | + const results = []; |
| 67 | + lines.forEach((line) => { |
| 68 | + // Ignore code blocks |
| 69 | + if (line.startsWith('```')) { |
| 70 | + inCode = !inCode; |
| 71 | + results.push(line); |
| 72 | + return; |
| 73 | + } |
| 74 | + if (inCode) { |
| 75 | + results.push(line); |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + results.push(addHeaderID(line, slugger)); |
| 80 | + }); |
| 81 | + return results; |
| 82 | +} |
| 83 | + |
| 84 | +async function main(paths) { |
| 85 | + paths = paths.length === 0 ? ['src/pages'] : paths; |
| 86 | + |
| 87 | + const [unifiedMod, remarkParseMod, remarkSlugMod] = await Promise.all([ |
| 88 | + import('unified'), |
| 89 | + import('remark-parse'), |
| 90 | + import('remark-slug'), |
| 91 | + ]); |
| 92 | + const unified = unifiedMod.default; |
| 93 | + const remarkParse = remarkParseMod.default; |
| 94 | + const remarkSlug = remarkSlugMod.default; |
| 95 | + modules = {unified, remarkParse, remarkSlug}; |
| 96 | + const files = paths.map((path) => [...walk(path)]).flat(); |
| 97 | + |
| 98 | + files.forEach((file) => { |
| 99 | + if (!(file.endsWith('.md') || file.endsWith('.mdx'))) { |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + const content = fs.readFileSync(file, 'utf8'); |
| 104 | + const lines = content.split('\n'); |
| 105 | + const updatedLines = addHeaderIDs(lines); |
| 106 | + fs.writeFileSync(file, updatedLines.join('\n')); |
| 107 | + }); |
| 108 | +} |
| 109 | + |
| 110 | +module.exports = main; |
0 commit comments