-
Notifications
You must be signed in to change notification settings - Fork 513
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
fix(build): check for bad_src flaws in markdown files #8133
Conversation
build/check-images.ts
Outdated
const matches = isMarkdown | ||
? findMatchesInMarkdown(rawContent, "image", src) | ||
: findMatchesInText(src, rawContent, { | ||
attribute: "src", | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As we still have many HTML tables in content. Is it necessary to match both in Markdown and Text for markdown files? (but we may need to ignore the html codes in <pre>
example: mdn/content#24154
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, indeed, the HTML table on the cursor page is not going away anytime soon, so we should also findMatchesInText()
for Markdown files.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @caugner, as we need to sort the matches, so the codes might be:
const matches = findMatchesInText(src, rawContent, {
attribute: "src",
});
if (isMarkdown) {
const hasMatchInHTML = matches.length > 0;
matches.push(
...findMatchesInMarkdown(src, rawContent, { type: "image" })
);
// note that, we need to sort them by line and column
if (hasMatchInHTML) {
matches.sort((a, b) =>
a.line === b.line ? a.column - b.column : a.line - b.line
);
}
}
I'm not sure could we call findMatchesInText
function in findMatchesInMarkdown
as we could deal with broken html format links in markdown file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Found that the fromMarkdown
can also parse html nodes: see AST.
Resolved this by also parse HTML node in markdown
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, just some nits.
build/check-images.ts
Outdated
const matches = isMarkdown | ||
? findMatchesInMarkdown(rawContent, "image", src) | ||
: findMatchesInText(src, rawContent, { | ||
attribute: "src", | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, indeed, the HTML table on the cursor page is not going away anytime soon, so we should also findMatchesInText()
for Markdown files.
@@ -1070,6 +1070,21 @@ test("image flaws with bad images", () => { | |||
"File not present on disk, an empty file, or not an image" | |||
).length | |||
).toBe(4); | |||
// Check the line and column numbers for html <img> tags in markdown |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trying to fix the testing error in mdn/content#24265 |
This pull request has merge conflicts that must be resolved before it can be merged. |
This pull request has merge conflicts that must be resolved before it can be merged. |
This pull request has merge conflicts that must be resolved before it can be merged. |
This pull request has merge conflicts that must be resolved before it can be merged. |
This pull request has merge conflicts that must be resolved before it can be merged. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
@yin1999 Can you double-check that it still works as expected?
Hey @caugner. This still works, I've only found the fix flow functionality does not work for HTML code in markdown file, as the Addition: I've add dev dependency |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, just tested it locally once more. Great work! 🎉
According to the documentation [1], type imports can be registered by using `@types/mdast`. And we have already added this dev-dependency, so remove the import of `mdast-util-directive`. Removed the dependency added in mdn#8133. [1]: https://www.npmjs.com/package/mdast-util-directive#types
According to the documentation [1], type imports can be registered by using `@types/mdast`. And we have already added this dev-dependency, so remove the import of `mdast-util-directive`. Removed the dependency added in #8133. [1]: https://www.npmjs.com/package/mdast-util-directive#types
Summary
As we are 100% markdown now, but the images check will only match src in HTML files (see function
findMatchesInText
).And we already have a function
findMatchesInMarkdown
, which is used to match links in markdown. I just reuse this function to match image's src. So we could show bad src flaws for markdown files.Also as a part of #7574, for the bad_image test will be failed after convert testing files to markdown.
Problem
The bad_src flaws for image files are only works for html files. So we couldn't be notified when there is a bad image src within a markdown document.
Solution
check whether the document is a markdown file. If so, use
findMatchesInMarkdown
to match image's src.To make this be a test, convert the bad_image test file to markdown (as we are 100% markdown now).
Screenshots
Try to add a image (with bad image src) in document:
files/zh-cn/mdn/at_ten/index.md
Then, run
yarn dev
and openhttp://localhost:5042/zh-CN/docs/MDN/At_ten/index.json
Before
We could not see the bad_src flaws
After
We could see the bad_src flaws
How did you test this change?
run
yarn dev
andyarn test