mirror of
https://github.com/reactjs/react.dev.git
synced 2026-02-24 20:53:08 +00:00
Co-authored-by: Dan Abramov <dan.abramov@me.com> Co-authored-by: Sylwia Vargas <sylwia.vargas@gmail.com> Co-authored-by: Dan Lebowitz <dan.lebo@me.com> Co-authored-by: Razvan Gradinar <grazvan@fb.com> Co-authored-by: Jared Palmer <jared@palmer.net> Co-authored-by: Dane Grant <danecando@gmail.com> Co-authored-by: Dustin Goodman <dustin.s.goodman@gmail.com> Co-authored-by: Rick Hanlon <rickhanlonii@gmail.com> Co-authored-by: Maggie Appleton <maggie.fm.appleton@gmail.com> Co-authored-by: Alex Moldovan <alex.n.moldovan@gmail.com> Co-authored-by: Ives van Hoorne <ives.v.h@gmail.com> Co-authored-by: Brian Vaughn <bvaughn@fb.com>
80 lines
1.8 KiB
JavaScript
80 lines
1.8 KiB
JavaScript
/**
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*/
|
|
|
|
const fs = require('fs');
|
|
const GithubSlugger = require('github-slugger');
|
|
|
|
function walk(dir) {
|
|
let results = [];
|
|
const list = fs.readdirSync(dir);
|
|
list.forEach(function (file) {
|
|
file = dir + '/' + file;
|
|
const stat = fs.statSync(file);
|
|
if (stat && stat.isDirectory()) {
|
|
/* Recurse into a subdirectory */
|
|
results = results.concat(walk(file));
|
|
} else {
|
|
/* Is a file */
|
|
results.push(file);
|
|
}
|
|
});
|
|
return results;
|
|
}
|
|
|
|
function stripLinks(line) {
|
|
return line.replace(/\[([^\]]+)\]\([^)]+\)/, (match, p1) => p1);
|
|
}
|
|
|
|
function addHeaderID(line, slugger) {
|
|
// check if we're a header at all
|
|
if (!line.startsWith('#')) {
|
|
return line;
|
|
}
|
|
// check if it already has an id
|
|
if (/\{#[^}]+\}/.test(line)) {
|
|
return line;
|
|
}
|
|
const headingText = line.slice(line.indexOf(' ')).trim();
|
|
const headingLevel = line.slice(0, line.indexOf(' '));
|
|
return `${headingLevel} ${headingText} {#${slugger.slug(
|
|
stripLinks(headingText)
|
|
)}}`;
|
|
}
|
|
|
|
function addHeaderIDs(lines) {
|
|
// Sluggers should be per file
|
|
const slugger = new GithubSlugger();
|
|
let inCode = false;
|
|
const results = [];
|
|
lines.forEach((line) => {
|
|
// Ignore code blocks
|
|
if (line.startsWith('```')) {
|
|
inCode = !inCode;
|
|
results.push(line);
|
|
return;
|
|
}
|
|
if (inCode) {
|
|
results.push(line);
|
|
return;
|
|
}
|
|
|
|
results.push(addHeaderID(line, slugger));
|
|
});
|
|
return results;
|
|
}
|
|
|
|
const [path] = process.argv.slice(2);
|
|
|
|
const files = walk(path);
|
|
files.forEach((file) => {
|
|
if (!(file.endsWith('.md') || file.endsWith('.mdx'))) {
|
|
return;
|
|
}
|
|
|
|
const content = fs.readFileSync(file, 'utf8');
|
|
const lines = content.split('\n');
|
|
const updatedLines = addHeaderIDs(lines);
|
|
fs.writeFileSync(file, updatedLines.join('\n'));
|
|
});
|