forked from MapComplete/MapComplete
		
	chore: improve script to extract questionhints
This commit is contained in:
		
							parent
							
								
									35d12a653d
								
							
						
					
					
						commit
						12ac229d1b
					
				
					 2 changed files with 25 additions and 14 deletions
				
			
		| 
						 | 
					@ -613,20 +613,21 @@ 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 html = document.createElement("p")
 | 
				
			||||||
 | 
					                html.innerHTML = question.textFor(lng)
 | 
				
			||||||
                const divs = Array.from(html.getElementsByTagName("div"))
 | 
					                const divs = Array.from(html.getElementsByTagName("div"))
 | 
				
			||||||
                const spans = Array.from(html.getElementsByTagName("span"))
 | 
					                const spans = Array.from(html.getElementsByTagName("span"))
 | 
				
			||||||
                const brs = Array.from(html.getElementsByTagName("br"))
 | 
					                const brs = Array.from(html.getElementsByTagName("br"))
 | 
				
			||||||
                const subtles = Array.from(html.getElementsByClassName("subtle"))
 | 
					                const subtles = Array.from(html.getElementsByClassName("subtle"))
 | 
				
			||||||
                if (divs.length + spans.length + brs.length + subtles.length > 0) {
 | 
					                if (divs.length + spans.length + brs.length + subtles.length > 0) {
 | 
				
			||||||
                    warnings.push(
 | 
					                    warnings.push(
 | 
				
			||||||
                    "At " +
 | 
					                        `At ${context}: the question for ${lng} contains a div, a span, a br or an element with class 'subtle'. Please, use a \`questionHint\` instead.
 | 
				
			||||||
                        context +
 | 
					    The question is: ${question.textFor(lng)}`
 | 
				
			||||||
                        ": 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")
 | 
					 | 
				
			||||||
                    )
 | 
					                    )
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
        return {
 | 
					        return {
 | 
				
			||||||
            result: json,
 | 
					            result: json,
 | 
				
			||||||
            errors,
 | 
					            errors,
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue