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

View file

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