Skip to content

Commit 0f2c993

Browse files
committed
feat(remark): Include page title in auto-detection
The remark plugin now correctly parses the frontmatter title of a page and includes it in the text scanned for antibiotic and organism synonyms. This allows the `auto` parameter to resolve terms that are only present in the page title. It fixes an issue where `file.data.frontmatter` was not yet populated during the remark processing stage by using `gray-matter` to parse the file content directly.
1 parent 122881c commit 0f2c993

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

src/remark/index.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { toString } from "mdast-util-to-string";
33
import { getSharedData, resolveIds, selectDataSource, loadResistanceDataForSource } from "../data";
44
import { join } from "path";
55
import chalk from 'chalk';
6+
import matter from 'gray-matter';
7+
import fs from 'fs';
68

79
// AST → Plaintext (robust for MD/MDX, excluding code, inlineCode, and %%RESIST paragraphs)
810
export function mdastToPlainText(root: any): string {
@@ -109,11 +111,18 @@ export default function remarkResistogram(options: { dataDir?: string, files?: a
109111
let pageText = mdastToPlainText(tree);
110112

111113
// --- Add title from frontmatter if present -------------------------------
112-
// In Docusaurus, parsed frontmatter is available on file.data.frontmatter
113-
const fm = (file.data && (file.data as any).frontmatter) || {};
114-
if (fm.title) {
115-
// Surround with spaces so word-boundary regexes still work
116-
pageText = ` ${fm.title} ${pageText}`;
114+
// Manually read the file and parse frontmatter using gray-matter
115+
// because file.data.frontmatter might not be reliably populated
116+
// at this stage of the Docusaurus build process.
117+
try {
118+
const fileContent = fs.readFileSync(file.path, 'utf8');
119+
const { data: fm } = matter(fileContent);
120+
if (fm.title) {
121+
// Surround with spaces so word-boundary regexes still work
122+
pageText = ` ${fm.title} ${pageText}`;
123+
}
124+
} catch (e) {
125+
// Ignore errors (e.g., file not found), proceed with AST-based text.
117126
}
118127

119128
const nodesToProcess: any[] = [];

0 commit comments

Comments
 (0)