-
Notifications
You must be signed in to change notification settings - Fork 11
Add markdown check for absolute URLs in links to previous posts #541
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,10 @@ | |
| * Current checks: | ||
| * - H1 headings (# ...): the H1 is already generated from the `title` field | ||
| * in the frontmatter, so adding a `#` heading manually creates a duplicate. | ||
| * - Absolute URLs to the published site: links beginning with | ||
| * https://design-history.prevention-services.nhs.uk/ should use | ||
| * relative URLs instead, so that they work in previews and in case the URL | ||
| * changes in future. | ||
| * | ||
| * When run directly, scans all markdown files under app/: | ||
| * npm run check:markdown | ||
|
|
@@ -24,6 +28,13 @@ const H1_MESSAGE = | |
| 'If this heading duplicates the title, remove it. ' + | ||
| 'If it is a different heading, change it to an H2 using `##`.' | ||
|
|
||
| const SITE_URL = 'https://design-history.prevention-services.nhs.uk/' | ||
|
|
||
| const ABSOLUTE_URL_MESSAGE = | ||
| 'Use a relative URL instead of a full URL for links to other posts on the site.\n\n' + | ||
| 'This means that the links will work in previews, and in case the site domain name changes in future.\n\n' + | ||
| 'For example, replace `https://design-history.prevention-services.nhs.uk/some/path/` with `/some/path/`.' | ||
|
|
||
| /** | ||
| * Recursively finds all .md files under the given directory. | ||
| * | ||
|
|
@@ -56,6 +67,9 @@ export function scanAllFiles() { | |
| if (/^# /.test(lines[i])) { | ||
| mistakes.push({ path: filePath, line: i + 1, message: H1_MESSAGE }) | ||
| } | ||
| if (lines[i].includes('](' + SITE_URL)) { | ||
| mistakes.push({ path: filePath, line: i + 1, message: ABSOLUTE_URL_MESSAGE }) | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this will fail to work if |
||
| } | ||
| } | ||
|
|
||
|
|
@@ -106,6 +120,16 @@ export function getMistakes(baseRef) { | |
| message: H1_MESSAGE | ||
| }) | ||
| } | ||
| // Added line containing an absolute URL to the published site | ||
| const lineContent = rawLine.slice(1) | ||
| if (lineContent.includes('](' + SITE_URL)) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This syntax seems really weird. Is it even valid?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I now realise this is string concatenation. But I find it really hard to follow / it didn't look valid. Better would be string literal, though better still some variable that's described with a comment or regex. |
||
| mistakes.push({ | ||
| path: currentFile, | ||
| line: lineNumber, | ||
| message: ABSOLUTE_URL_MESSAGE, | ||
| suggestion: lineContent.replaceAll(SITE_URL, '/') | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might this fail to work if they've not included the
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point. @copilot update the code so that it also works if the |
||
| }) | ||
| } | ||
| } else if (!rawLine.startsWith('-')) { | ||
| // Context line -- still advances the new-file line number | ||
| lineNumber++ | ||
|
|
||
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.
This syntax is hard to follow
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.
I now realise this is string concatenation. But I find it really hard to follow / it didn't look valid. Better would be string literal, though better still some variable that's described with a comment or regex.
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.
This also won't work if there's a space in the markdown - eg
[ Link text ]( url ), differences in casing (unlikely), if they don't includehttps://, or if it's an html link.