Skip to content

Commit

Permalink
Fixes an issue discovered in dotnet/docs#38912 (#283)
Browse files Browse the repository at this point in the history
* Fixes an issue discovered in dotnet/docs#38912

* Fix MD lint issue in sample.md
  • Loading branch information
IEvangelist authored Jan 3, 2024
1 parent 5e29200 commit 880b216
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe("file-heading-extractor", () => {
it("when calling getHeadingTextFrom correctly returns H1 value.", async () => {
const path = "__tests__/sample.md";
const actual = await getHeadingTextFrom(path);
expect(actual).toBe("The heading");
expect(actual).toBe("The heading `System.Console` class");
});

it("when calling getHeadingTextFrom correctly returns title value.", async () => {
Expand Down
18 changes: 9 additions & 9 deletions actions/status-checker/__tests__/sample.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Not heading

# The heading
# The heading <xref:System.Console> class

An h1 header
============
Expand All @@ -10,9 +10,9 @@ Paragraphs are separated by a blank line.
2nd paragraph. *Italic*, **bold**, and `monospace`. Itemized lists
look like:

* this one
* that one
* the other one
- this one
- that one
- the other one

Note that --- not considering the asterisk --- the actual text
content starts at 4-columns in.
Expand All @@ -39,8 +39,8 @@ Here's a numbered list:
Note again how the actual text starts at 4 columns in (4 characters
from the left side). Here's a code sample:

# Let me re-iterate ...
for i in 1 .. 10 { do-something(i) }
# Let me re-iterate ...
for i in 1 .. 10 { do-something(i) }

As you probably guessed, indented 4 spaces. By the way, instead of
indenting the block, you can use delimited blocks, if you like:
Expand Down Expand Up @@ -68,9 +68,9 @@ Now a nested list:

1. First, get these ingredients:

* carrots
* celery
* lentils
- carrots
- celery
- lentils

2. Boil some water.

Expand Down
18 changes: 17 additions & 1 deletion actions/status-checker/dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion actions/status-checker/dist/index.js.map

Large diffs are not rendered by default.

23 changes: 22 additions & 1 deletion actions/status-checker/src/file-heading-extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@ export async function getHeadingTextFrom(path: string): Promise<string | null> {
return null;
}

const result: string | null =
let result: string | null =
tryGetRegExpMatch(h1RegExp, "h1", content) ??
tryGetRegExpMatch(titleRegExp, "title", content);

console.log(`Found ${result} from '${path}' contents.`);

if (result && result.indexOf("<xref:") > -1) {
result = normalizeHeadingOrTitleText(result);
console.log(` normalized as ${result}`);
}

return result;
} catch (error) {
if (error) {
Expand All @@ -29,6 +34,22 @@ export async function getHeadingTextFrom(path: string): Promise<string | null> {
return null;
}

const xrefRegExp = /<xref:([^>]+)>/gim;

function normalizeHeadingOrTitleText(headingText: string): string {
// If contains xref markdown, extract only the text from it.
// Example: "<xref:System.Globalization.CompareInfo> class"
// or "<xref:System.Globalization.CompareInfo /> class"
// Result: "`System.Globalization.CompareInfo` class"
const xrefMatch = xrefRegExp.exec(headingText);

if (xrefMatch && xrefMatch[1]) {
headingText = headingText.replace(xrefRegExp, `\`${xrefMatch[1]}\``);
}

return headingText;
}

function tryGetRegExpMatch(
expression: RegExp,
groupName: string,
Expand Down

0 comments on commit 880b216

Please sign in to comment.