-
Notifications
You must be signed in to change notification settings - Fork 126
Add check that page title is in sync with ToC, h1, and metadata #3669
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
GantaRoja
wants to merge
11
commits into
main
Choose a base branch
from
Roja/CheckPageTotleSync
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
fb15293
Added a new function and test case for mismatched title and heading
GantaRoja 176bc95
lint errors fixed
GantaRoja 9ab781c
separaeted tests removed consoles
GantaRoja 32cfd9d
lint error fixed
GantaRoja 5b5fd21
called the function and rename of checkimages to checkMarkdoen
GantaRoja 86b5296
checkpoint
GantaRoja 4e6ac5f
modified check file
GantaRoja 94d1e01
corrected packagejson file
GantaRoja 6a9af5b
ignored files starts with doc/spi
GantaRoja 8012257
moved parsing step out of the function
GantaRoja ee77032
moved helper function to a new file
GantaRoja File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// This code is a Qiskit project. | ||
// | ||
// (C) Copyright IBM 2023. | ||
frankharkins marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// | ||
// 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 for matching titles and headings", async () => { | ||
const markdown1 = `--- | ||
title: My Awesome Guide | ||
--- | ||
|
||
# My Awesome Guide | ||
`; | ||
const mismatched = await collectHeadingTitleMismatch(markdown1); | ||
const result: string[] = []; | ||
expect(mismatched).toEqual(result); | ||
}); | ||
|
||
test("Test to find mismatched titles and headings", async () => { | ||
const markdown2 = `--- | ||
title: Qiskit Doc | ||
author: John | ||
--- | ||
|
||
# Introduction | ||
|
||
This guide will walk you through everything.`; | ||
|
||
const mismatched2 = await collectHeadingTitleMismatch(markdown2); | ||
|
||
const result2: string[] = [ | ||
`Mismatch: frontmatter title "Qiskit Doc" does not match heading "Introduction"`, | ||
]; | ||
|
||
expect(mismatched2).toEqual(result2); | ||
}); | ||
|
||
test("Test to mismatched and complex titles and headings", async () => { | ||
const markdown2 = `--- | ||
title: My Awesome Guide | ||
--- | ||
|
||
# This is a *Heading* | ||
|
||
This guide will walk you through everything.`; | ||
|
||
const mismatched2 = await collectHeadingTitleMismatch(markdown2); | ||
|
||
const result2: string[] = [ | ||
`Mismatch: frontmatter title "My Awesome Guide" does not match heading "This is a Heading"`, | ||
]; | ||
|
||
expect(mismatched2).toEqual(result2); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { unified } from "unified"; | ||
frankharkins marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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"; | ||
|
||
// Helper to recursively extract visible text from heading node | ||
function extractText(node: any): string { | ||
if (node.type === "text" || node.type === "inlineCode") { | ||
return node.value; | ||
} | ||
|
||
if (node.type === "link" && node.children) { | ||
return node.children.map(extractText).join(" "); | ||
} | ||
|
||
if (node.children && Array.isArray(node.children)) { | ||
return node.children.map(extractText).join(" "); | ||
} | ||
|
||
return ""; | ||
} | ||
|
||
export async function collectHeadingTitleMismatch( | ||
markdown: string, | ||
): Promise<string[]> { | ||
frankharkins marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const mismatches = new Set<string>(); | ||
|
||
let frontmatterTitle: string | undefined; | ||
let headingText: string | undefined; | ||
|
||
const processor = unified() | ||
.use(remarkParse) | ||
.use(remarkGfm) | ||
.use(remarkFrontmatter, ["yaml"]); | ||
|
||
const tree = processor.parse(markdown); | ||
frankharkins marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Extract frontmatter title | ||
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; | ||
frankharkins marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
}); | ||
|
||
// Extract first level-1 heading with full formatting | ||
visit(tree, "heading", (node: any) => { | ||
if (node.depth === 1 && !headingText) { | ||
headingText = extractText(node).trim(); | ||
} | ||
}); | ||
|
||
// Compare and collect mismatch | ||
if (frontmatterTitle && headingText && frontmatterTitle !== headingText) { | ||
mismatches.add( | ||
`Mismatch: frontmatter title "${frontmatterTitle}" does not match heading "${headingText}"`, | ||
); | ||
} | ||
|
||
return Array.from(mismatches); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.