Skip to content

Commit bfae031

Browse files
fix: throw error if invalid link syntax is detected
1 parent 3d25e27 commit bfae031

2 files changed

Lines changed: 64 additions & 0 deletions

File tree

packages/docs-builder/src/parse.spec.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,4 +275,49 @@ there
275275
'Unresolved reference-style link found for lang=en link=[This is a link][unknown_ref]'
276276
)
277277
})
278+
279+
it('should throw an error if invalid link syntax is detected', () => {
280+
const links = `\
281+
This is a valid normal link: [page](https://climateinteractive.org)
282+
283+
This is a valid reference-style link: [page][ref]
284+
285+
This is an invalid normal link: [page] (https://climateinteractive.org)
286+
287+
This is an invalid reference-style link: [page] [ref]
288+
`
289+
290+
const md = `\
291+
# <!-- section:section_1 -->Section 1
292+
293+
<!-- begin-def:block_1 -->
294+
295+
${links}
296+
297+
<!-- end-def -->
298+
299+
[ref]: https://climateinteractive.org
300+
`
301+
302+
function expectedMsg(lang: string) {
303+
const langPart = lang === 'en' ? '' : `lang=${lang} `
304+
return `\
305+
Detected invalid link syntax:
306+
[page] (https://climateinteractive.org)
307+
[page] [ref]
308+
To fix, ensure there are no spaces between link text and link url/reference, for example: [text](url) or [text][ref] (${langPart}page=page_1.md scope=section_1)`
309+
}
310+
311+
// Verify that an error is thrown if the English content contains invalid link syntax
312+
const enContext = new Context(config, 'en')
313+
expect(() => parseMarkdownPageContent(enContext, 'page_1.md', md)).toThrow(expectedMsg('en'))
314+
315+
// Verify that an error is thrown if the translated content contains invalid link syntax
316+
const deBlocks = new Map([
317+
['section_1__title', 'Section 1'],
318+
['section_1__block_1', links]
319+
])
320+
const deContext = new Context(config, 'de', undefined, deBlocks)
321+
expect(() => parseMarkdownPageContent(deContext, 'page_1.md', md)).toThrow(expectedMsg('de'))
322+
})
278323
})

packages/docs-builder/src/parse.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,7 @@ function addBlockForTokens(
517517
sep = '\n\n'
518518
}
519519
const blockText = blockParts.join(sep)
520+
checkForInvalidLinkSyntax(context, blockText)
520521
context.addBlock(localBlockId, blockText)
521522
return tokens
522523
} else {
@@ -525,6 +526,7 @@ function addBlockForTokens(
525526
const blockText = context.getTranslatedBlockText(fullBlockId)
526527
if (blockText) {
527528
// Parse the translated tokens and insert them
529+
checkForInvalidLinkSyntax(context, blockText)
528530
// XXX: If there is more than one token (even if some are whitespace only), we
529531
// need to include extra newlines after the last block, otherwise marked will
530532
// not parse the text correctly
@@ -539,6 +541,23 @@ function addBlockForTokens(
539541
}
540542
}
541543

544+
/**
545+
* Throw an error if the given text contains invalid link syntax.
546+
*/
547+
function checkForInvalidLinkSyntax(context: Context, md: string): void {
548+
const invalidLinkRegExp = /\[([^\]]+)\]\s+\(([^)]+)\)|\[([^\]]+)\]\s+\[([^\]]+)\]/g
549+
const matches = md.match(invalidLinkRegExp)
550+
if (matches) {
551+
let msg = 'Detected invalid link syntax:\n'
552+
for (const match of matches) {
553+
msg += `${match}\n`
554+
}
555+
msg +=
556+
'To fix, ensure there are no spaces between link text and link url/reference, for example: [text](url) or [text][ref]'
557+
throw new Error(context.getScopedMessage(msg))
558+
}
559+
}
560+
542561
/**
543562
* Log a warning if the given text token is on a translatable page but is
544563
* not included in a `def` or `begin/end-def` pair.

0 commit comments

Comments
 (0)