chore: improve script to extract questionhints

This commit is contained in:
Pieter Vander Vennet 2023-03-08 03:37:48 +01:00
parent 35d12a653d
commit 12ac229d1b
2 changed files with 25 additions and 14 deletions

View file

@ -613,18 +613,19 @@ class MiscTagRenderingChecks extends DesugaringStep<TagRenderingConfigJson> {
new TypedTranslation(json["question"]),
context + ".question"
)
const html = question.ConstructElement()
const divs = Array.from(html.getElementsByTagName("div"))
const spans = Array.from(html.getElementsByTagName("span"))
const brs = Array.from(html.getElementsByTagName("br"))
const subtles = Array.from(html.getElementsByClassName("subtle"))
if (divs.length + spans.length + brs.length + subtles.length > 0) {
warnings.push(
"At " +
context +
": the question contains a div, a span, a br or an element with class 'subtle'. Please, use a `questionHint` instead.\n The question is: " +
question.textFor("en")
)
for (const lng of question.SupportedLanguages()) {
const html = document.createElement("p")
html.innerHTML = question.textFor(lng)
const divs = Array.from(html.getElementsByTagName("div"))
const spans = Array.from(html.getElementsByTagName("span"))
const brs = Array.from(html.getElementsByTagName("br"))
const subtles = Array.from(html.getElementsByClassName("subtle"))
if (divs.length + spans.length + brs.length + subtles.length > 0) {
warnings.push(
`At ${context}: the question for ${lng} contains a div, a span, a br or an element with class 'subtle'. Please, use a \`questionHint\` instead.
The question is: ${question.textFor(lng)}`
)
}
}
}
return {

View file

@ -37,10 +37,20 @@ class ExtractQuestionHint extends DesugaringStep<QuestionableTagRenderingConfigJ
const hint: Record<string, string> = {}
for (const language in json.question) {
const parts = json.question[language].split(/<br ?\/>/i)
const q = json.question[language]
const parts = q.split(/<br ?\/>/i)
if (parts.length == 2) {
json.question[language] = parts[0]
hint[language] = new FixedUiElement(parts[1]).ConstructElement().textContent
continue
}
const divStart = q.indexOf("<div")
if (divStart > 0) {
json.question[language] = q.substring(0, divStart)
hint[language] = new FixedUiElement(
q.substring(divStart)
).ConstructElement().textContent
}
}
if (Object.keys(hint).length > 0) {
@ -83,7 +93,7 @@ class FixQuestionHint extends Script {
}
const layers: LayerConfigJson[] = contents["layers"] ?? [contents]
for (const layer of layers) {
for (let i = 0; i < layer.tagRenderings.length; i++) {
for (let i = 0; i < layer.tagRenderings?.length; i++) {
const tagRendering = layer.tagRenderings[i]
if (typeof tagRendering !== "object" || tagRendering["question"] === undefined) {
continue