Skip to content

Commit 47e101f

Browse files
committed
Keep links updated
1 parent 41544d4 commit 47e101f

3 files changed

Lines changed: 51 additions & 2 deletions

File tree

packages/utils/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ The package includes a powerful tree visualization utility that renders structur
403403

404404
| Function | Description |
405405
| ------------------------------------------------------------------------------------------------------ | ---------------------------------------------------------- |
406-
| [`inspect`](https://github.com/datocms/structured-text/blob/main/packages/utils/src/inspector.ts#L199) | Render a structured text document or node as an ASCII tree |
406+
| [`inspect`](https://github.com/datocms/structured-text/blob/main/packages/utils/src/inspector.ts#L202) | Render a structured text document or node as an ASCII tree |
407407

408408
```javascript
409409
import { inspect } from 'datocms-structured-text-utils';

packages/utils/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"url": "git+https://github.com/datocms/structured-text.git"
2727
},
2828
"scripts": {
29-
"build": "tsc && tsc --project ./tsconfig.esnext.json",
29+
"build": "node update-links.js && tsc && tsc --project ./tsconfig.esnext.json",
3030
"prebuild": "rimraf dist"
3131
},
3232
"bugs": {

packages/utils/update-links.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
const REPO_PREFIX =
5+
'https://github.com/datocms/structured-text/blob/main/packages/utils/';
6+
7+
const readmePath = path.join(process.cwd(), 'README.md');
8+
let readme = fs.readFileSync(readmePath, 'utf-8');
9+
10+
// Matches: [`symbol`](https://.../file#L123)
11+
const linkRegex = new RegExp(
12+
'\\[`([^`]+)`\\]\\(' + REPO_PREFIX + '([^#)]+)#L\\d+\\)',
13+
'g',
14+
);
15+
16+
function findLineNumber(filePath, symbol) {
17+
const content = fs.readFileSync(filePath, 'utf-8').split('\n');
18+
19+
// Look for `function foo`, `class Foo`, or `const foo =`
20+
const regex = new RegExp(
21+
`\\b(?:function|class|const|let|var)\\s+${symbol}\\b`,
22+
);
23+
24+
for (let i = 0; i < content.length; i++) {
25+
if (regex.test(content[i])) {
26+
return i + 1; // GitHub is 1-indexed
27+
}
28+
}
29+
return null;
30+
}
31+
32+
readme = readme.replace(linkRegex, (full, symbol, file) => {
33+
const absPath = path.join(process.cwd(), file);
34+
if (!fs.existsSync(absPath)) {
35+
console.warn(`⚠️ File not found: ${file}`);
36+
return full;
37+
}
38+
39+
const newLine = findLineNumber(absPath, symbol);
40+
if (!newLine) {
41+
console.warn(`⚠️ Could not locate ${symbol} in ${file}`);
42+
return full;
43+
}
44+
45+
return `[\`${symbol}\`](${REPO_PREFIX}${file}#L${newLine})`;
46+
});
47+
48+
fs.writeFileSync(readmePath, readme);
49+
console.log('✅ README GitHub links updated!');

0 commit comments

Comments
 (0)