Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
24 changes: 24 additions & 0 deletions .github/scripts/check-markdown.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
*
Expand Down Expand Up @@ -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)) {

Copy link
Copy Markdown
Collaborator

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

Copy link
Copy Markdown
Collaborator

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.

Copy link
Copy Markdown
Collaborator

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 include https://, or if it's an html link.

mistakes.push({ path: filePath, line: i + 1, message: ABSOLUTE_URL_MESSAGE })
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will fail to work if https:// is not included

}
}

Expand Down Expand Up @@ -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)) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This syntax seems really weird. Is it even valid?

Copy link
Copy Markdown
Collaborator

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.

mistakes.push({
path: currentFile,
line: lineNumber,
message: ABSOLUTE_URL_MESSAGE,
suggestion: lineContent.replaceAll(SITE_URL, '/')

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might this fail to work if they've not included the https:// bit?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The 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 https:// prefix has been dropped, or if http:// is used.

})
}
} else if (!rawLine.startsWith('-')) {
// Context line -- still advances the new-file line number
lineNumber++
Expand Down
26 changes: 19 additions & 7 deletions .github/scripts/pr-review.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ const { GITHUB_TOKEN, REPO, BASE_REF, PR_NUMBER, HEAD_SHA } = process.env

const BOT_USER = 'github-actions[bot]'

/**
* Builds the comment body for a mistake. If the mistake includes a suggestion,
* appends a GitHub suggestion block so the author can apply the fix in one click.
*
* @param {{ message: string, suggestion?: string }} mistake
* @returns {string}
*/
function commentBody({ message, suggestion }) {
if (!suggestion) return message
return `${message}\n\n\`\`\`suggestion\n${suggestion}\n\`\`\``
}

async function githubFetch(path, options = {}) {
const response = await fetch(`https://api.github.com${path}`, {
...options,
Expand Down Expand Up @@ -56,7 +68,7 @@ const botComments = existingComments
const staleComments = botComments.filter(
(c) =>
!mistakes.some(
(m) => m.path === c.path && m.line === c.line && m.message === c.body
(m) => m.path === c.path && m.line === c.line && commentBody(m) === c.body
)
)

Expand Down Expand Up @@ -107,16 +119,16 @@ if (mistakes.length === 0) {
// Post new comments for mistakes that don't already have a comment
const newComments = mistakes
.filter(
({ path, line, message }) =>
(m) =>
!botComments.some(
(c) => c.path === path && c.line === line && c.body === message
(c) => c.path === m.path && c.line === m.line && c.body === commentBody(m)
)
)
.map(({ path, line, message }) => ({
path,
line,
.map((m) => ({
path: m.path,
line: m.line,
side: 'RIGHT',
body: message
body: commentBody(m)
}))

if (newComments.length === 0) {
Expand Down