Skip to content

Commit

Permalink
Warn on internal links with imperfect quotation marks (github#35042)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbe authored Mar 10, 2023
1 parent 4120d6b commit c748665
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 8 deletions.
46 changes: 40 additions & 6 deletions lib/update-internal-links.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ async function updateFile(file, context, opts) {
const ast = fromMarkdown(newContent)

const replacements = []
const skips = []
const warnings = []

// The day we know with confidence that everyone us on Node >=17,
// we can change this to use `structuredClone` without the polyfill
Expand Down Expand Up @@ -204,10 +204,36 @@ async function updateFile(file, context, opts) {
newTitle = AUTOTITLE
}
} else {
skips.push({
reason: 'Not leading by a quotation (")',
asMarkdown,
})
/**
* The Markdown link sometimes is written like this:
*
* ["This is the title](/foo/bar)."
*
* or...
*
* ["This is the title"](/foo/bar).
*/
if (node.children && node.children.length > 0 && node.children[0].value) {
if (singleStartingQuote(node.children[0].value)) {
const column = node.position.start.column
const line = node.position.start.line + lineOffset
warnings.push({
warning: 'Starts with a single " inside the text',
asMarkdown,
line,
column,
})
} else if (isSimpleQuote(node.children[0].value)) {
const column = node.position.start.column
const line = node.position.start.line + lineOffset
warnings.push({
warning: 'Starts and ends with a " inside the text',
asMarkdown,
line,
column,
})
}
}
}
}
if (opts.fixHref) {
Expand Down Expand Up @@ -244,8 +270,8 @@ async function updateFile(file, context, opts) {
rawContent,
newContent,
replacements,
warnings,
newData,
skips,
}
}

Expand Down Expand Up @@ -471,3 +497,11 @@ function getNewHref(href, context, opts, file) {
}
return newHref
}

function singleStartingQuote(text) {
return text.startsWith('"') && text.split('"').length === 2
}

function isSimpleQuote(text) {
return text.startsWith('"') && text.endsWith('"') && text.split('"').length === 3
}
36 changes: 34 additions & 2 deletions script/update-internal-links.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,16 @@ async function main(files, opts) {
const results = await updateInternalLinks(actualFiles, options)

let exitCheck = 0
for (const { file, rawContent, content, newContent, replacements, data, newData } of results) {
for (const {
file,
rawContent,
content,
newContent,
replacements,
data,
newData,
warnings,
} of results) {
const differentContent = content !== newContent
const differentData = !equalObject(data, newData)
if (differentContent || differentData) {
Expand Down Expand Up @@ -130,6 +139,14 @@ async function main(files, opts) {
)
}
}
if (warnings.length) {
console.log('Warnings...', chalk.bold(file))
for (const { warning, asMarkdown, line, column } of warnings) {
console.log(' ', chalk.yellow(asMarkdown))
console.log(' ', chalk.dim(`line ${line} column ${column}, ${warning}`))
console.log('')
}
}
}

if (opts.aggregateStats) {
Expand All @@ -147,7 +164,22 @@ async function main(files, opts) {
chalk.bold(countReplacements.toLocaleString())
)

countByTree(results)
const countWarnings = results.reduce((prev, next) => prev + next.warnings.length, 0)
const countWarningFiles = new Set(results.filter((result) => result.warnings.length > 0)).size
console.log(
'Number of files with warnings:'.padEnd(30),
chalk.bold(countWarningFiles.toLocaleString())
)
console.log('Sum number of warnings:'.padEnd(30), chalk.bold(countWarnings.toLocaleString()))

if (countWarnings > 0) {
console.log(chalk.yellow('\nNote! Warnings can currently not be automatically fixed.'))
console.log('Manually edit heeded warnings and run the script again to update.')
}

if (countChangedFiles > 0) {
countByTree(results)
}
}

if (exitCheck) {
Expand Down

0 comments on commit c748665

Please sign in to comment.