-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
misc: share markdown parsing in collect-strings and the report #9514
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b52ae97
misc: share markdown parsing in collect-strings and the report
brendankenny e2b102e
update markdown splitting interface
brendankenny 1860f8c
don't allow empty link text
brendankenny 7953ee3
whoops, delete
brendankenny e09a47c
grumble grumble feedback
brendankenny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ const path = require('path'); | |
const assert = require('assert'); | ||
const tsc = require('typescript'); | ||
const collectAndBakeCtcStrings = require('./bake-ctc-to-lhl.js'); | ||
const Util = require('../../report/html/renderer/util.js'); | ||
|
||
const LH_ROOT = path.join(__dirname, '../../../'); | ||
const UISTRINGS_REGEX = /UIStrings = .*?\};\n/s; | ||
|
@@ -157,28 +158,27 @@ function convertMessageToCtc(message, examples = {}) { | |
* @param {IncrementalCtc} icu | ||
*/ | ||
function _processPlaceholderMarkdownCode(icu) { | ||
const message = icu.message; | ||
|
||
// Check that number of backticks is even. | ||
const match = icu.message.match(/`/g); | ||
const match = message.match(/`/g); | ||
if (match && match.length % 2 !== 0) { | ||
throw Error(`Open backtick in message "${icu.message}"`); | ||
throw Error(`Open backtick in message "${message}"`); | ||
} | ||
|
||
// Split on backticked code spans | ||
const parts = icu.message.split(/`(.*?)`/g); | ||
icu.message = ''; | ||
let idx = 0; | ||
while (parts.length) { | ||
// Pop off the same number of elements as there are capture groups. | ||
const [preambleText, codeText] = parts.splice(0, 2); | ||
icu.message += preambleText; | ||
if (codeText) { | ||
for (const segment of Util.splitMarkdownCodeSpans(message)) { | ||
if (segment.isCode) { | ||
const placeholderName = `MARKDOWN_SNIPPET_${idx++}`; | ||
// Backtick replacement looks unreadable here, so .join() instead. | ||
icu.message += '$' + placeholderName + '$'; | ||
icu.placeholders[placeholderName] = { | ||
content: '`' + codeText + '`', | ||
example: codeText, | ||
content: '`' + segment.text + '`', | ||
example: segment.text, | ||
}; | ||
} else { | ||
icu.message += segment.text; | ||
} | ||
} | ||
} | ||
|
@@ -189,35 +189,39 @@ function _processPlaceholderMarkdownCode(icu) { | |
* @param {IncrementalCtc} icu | ||
*/ | ||
function _processPlaceholderMarkdownLink(icu) { | ||
const message = icu.message; | ||
|
||
// Check for markdown link common errors, ex: | ||
// * [extra] (space between brackets and parens) | ||
if (icu.message.match(/\[.*\] \(.*\)/)) { | ||
throw Error(`Bad Link syntax in message "${icu.message}"`); | ||
if (message.match(/\[.*\] \(.*\)/)) { | ||
throw Error(`Bad Link spacing in message "${message}"`); | ||
} | ||
// * [](empty link text) | ||
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. @exterkamp and I discussed this empty linkText case and decided while maybe there's some vague possibility of a need for invisible links at some point, it's almost certainly an accident today, so let's alert the author |
||
if (message.match(/\[\]\(.*\)/)) { | ||
throw Error(`markdown link text missing in message "${message}"`); | ||
} | ||
|
||
// Split on markdown links (e.g. [some link](https://...)). | ||
const parts = icu.message.split(/\[([^\]]*?)\]\((https?:\/\/.*?)\)/g); | ||
icu.message = ''; | ||
let idx = 0; | ||
|
||
while (parts.length) { | ||
// Pop off the same number of elements as there are capture groups. | ||
const [preambleText, linkText, linkHref] = parts.splice(0, 3); | ||
icu.message += preambleText; | ||
|
||
// Append link if there are any. | ||
if (linkText && linkHref) { | ||
const startPlaceholder = `LINK_START_${idx}`; | ||
const endPlaceholder = `LINK_END_${idx}`; | ||
icu.message += '$' + startPlaceholder + '$' + linkText + '$' + endPlaceholder + '$'; | ||
idx++; | ||
icu.placeholders[startPlaceholder] = { | ||
content: '[', | ||
}; | ||
icu.placeholders[endPlaceholder] = { | ||
content: `](${linkHref})`, | ||
}; | ||
for (const segment of Util.splitMarkdownLink(message)) { | ||
if (!segment.isLink) { | ||
// Plain text segment. | ||
icu.message += segment.text; | ||
continue; | ||
} | ||
|
||
// Otherwise, append any links found. | ||
const startPlaceholder = `LINK_START_${idx}`; | ||
const endPlaceholder = `LINK_END_${idx}`; | ||
icu.message += '$' + startPlaceholder + '$' + segment.text + '$' + endPlaceholder + '$'; | ||
idx++; | ||
icu.placeholders[startPlaceholder] = { | ||
content: '[', | ||
}; | ||
icu.placeholders[endPlaceholder] = { | ||
content: `](${segment.linkHref})`, | ||
}; | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 tried rewriting this with
i % 3
and have0
and1
cases, where the1
case also pops offparts[i + 1]
, but it just got harder to follow, so stuck with this internally