Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions scripts/js/lib/markdownTitles.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// This code is a Qiskit project.
//
// (C) Copyright IBM 2023.
//
// This code is licensed under the Apache License, Version 2.0. You may
// obtain a copy of this license in the LICENSE file in the root directory
// of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
//
// Any modifications or derivative works of this code must retain this
// copyright notice, and modified files need to carry a notice indicating
// that they have been altered from the originals.

import { expect, test } from "@playwright/test";

import { collectHeadingTitleMismatch } from "./markdownTitles";

test("Test to match mismatched titles and headings", async () => {
const markdown1 = `
---
title: My Awesome Guide
---

# My Awesome Guide
`;
const markdown2 = `---
title: Qiskit Doc
author: John
---

# Introduction

This guide will walk you through everything.`;
const mismatched = await collectHeadingTitleMismatch(markdown1);
const mismatched2 = await collectHeadingTitleMismatch(markdown2)
const result: string[] = [];
const result2: string[] = [`Mismatch: frontmatter title "Qiskit Doc" does not match heading "Introduction"`];
expect(mismatched).toEqual(result);
expect(mismatched2).toEqual(result2);
});
47 changes: 47 additions & 0 deletions scripts/js/lib/markdownTitles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { unified } from "unified";
import remarkParse from "remark-parse";
import remarkGfm from "remark-gfm";
import remarkFrontmatter from "remark-frontmatter";
import { visit } from "unist-util-visit";
import { Root } from "mdast";
import yaml from "js-yaml";

export async function collectHeadingTitleMismatch(markdown: string): Promise<string[]> {
const mismatches: string[] = [];
let frontmatterTitle: string | undefined;
let headingText: string | undefined;

const processor = unified()
.use(remarkParse)
.use(remarkGfm)
.use(remarkFrontmatter, ["yaml"]);

const tree = processor.parse(markdown);

// Run the transformer manually
visit(tree, "yaml", (node: any) => {
const data = yaml.load(node.value);
if (typeof data === "object" && data !== null && "title" in data) {
frontmatterTitle = (data as any).title;
console.log("Frontmatter title:", frontmatterTitle);
}
});

visit(tree, "heading", (node: any) => {
if (node.depth === 1 && !headingText) {
headingText = node.children
.filter((child: any) => child.type === "text")
.map((child: any) => child.value)
.join(" ");
console.log("Heading text:", headingText);
}
});

if (frontmatterTitle && headingText && frontmatterTitle !== headingText) {
mismatches.push(
`Mismatch: frontmatter title "${frontmatterTitle}" does not match heading "${headingText}"`
);
}

return mismatches;
}
Loading