Skip to content

Commit 1219166

Browse files
hkalodnerclaude
andcommitted
Include imported partial content in word counts
countWords() now resolves relative MDX/MD imports and inlines the partial file content before counting, giving accurate word counts for pages composed primarily of partials (e.g., glossary, intro). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 1b53ede commit 1219166

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

scripts/generate-doc-manifest.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,26 @@ function extractHeadings(content: string): [number, string][] {
153153
return headings;
154154
}
155155

156-
function countWords(content: string): number {
156+
function countWords(content: string, relPath?: string): number {
157+
// Resolve imported partials and inline their content
158+
let fullContent = content;
159+
if (relPath) {
160+
const importRegex = /import\s+\w+\s+from\s+['"]([^'"]+\.mdx?)['"]/g;
161+
let match: RegExpExecArray | null;
162+
while ((match = importRegex.exec(content)) !== null) {
163+
const importPath = match[1];
164+
if (importPath.startsWith('./') || importPath.startsWith('../')) {
165+
const fromDir = path.dirname(relPath);
166+
const abs = path.resolve(DOCS_ROOT, fromDir, importPath);
167+
if (fs.existsSync(abs)) {
168+
fullContent += '\n' + fs.readFileSync(abs, 'utf8');
169+
}
170+
}
171+
}
172+
}
157173
// Strip frontmatter delimiter lines, imports, JSX, and HTML tags
158-
const cleaned = content
159-
.replace(/^---[\s\S]*?---/m, '')
174+
const cleaned = fullContent
175+
.replace(/^---[\s\S]*?---/gm, '')
160176
.replace(/import\s+.+from\s+['"].+['"]/g, '')
161177
.replace(/<[^>]+>/g, '')
162178
.replace(/```[\s\S]*?```/g, '');
@@ -416,7 +432,7 @@ async function main() {
416432
sme: frontmatter.sme || undefined,
417433
},
418434
headings: extractHeadings(content),
419-
words: countWords(content),
435+
words: countWords(content, relPath),
420436
links: extractInternalLinks(content, relPath),
421437
extLinks: countExternalLinks(content),
422438
imports: extractImports(content, relPath),

0 commit comments

Comments
 (0)