algolia / algolia/diffable-html

Invalid multiline text node indentation

Abierto
#60 0 comentarios 0 reacciones 0 asignados Ver en GitHub
Lenguaje dominante
JavaScript
Estrellas
38
Forks
8
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

Currently for input:

```


My text
Another line


```

The output is:

```


My text
Another line


```

so first line is properly formatted (indentation corrected), but following lines are not.

This is an issue for me, because we are building HTML using different template strings, helpers etc, so indentation is essentially unpredictable. We use `diffable-html` in tests to compare expected HTML with actual HTML, and both expected and actual are built from template strings. Therefore, we need proper normalization.

I created patch that works for me:

```
diff --git a/dist/diffable-html.js b/dist/diffable-html.js
index c732d9e3eca544b70f1d10c8291881e3a5925c60..c4978caa2cbcc1064e33c65668feb86c22802fb7 100644
--- a/dist/diffable-html.js
+++ b/dist/diffable-html.js
@@ -206,9 +206,17 @@ var format = function(html, ref) {
return;
}

- appendLineBreak();
- appendCurrentIndentation();
- append(trimmed);
+ const splitted = text.split('\n');
+ for (const line of splitted) {
+ const lineTrimmed = line.trim();
+ if (lineTrimmed.length === 0) {
+ continue;
+ }
+
+ appendLineBreak();
+ appendCurrentIndentation();
+ append(lineTrimmed);
+ }
},
onclosetag: function(tagname) {
var isVoidTag = isVoidTagName(tagname);

```

Essentially it uses current logic for `onText` but applies it to each line. It seems it fixed my problem.
Note: this patch removes empty lines in text nodes.

Guía de contribución

No hay ninguna guía de contribución indexada para este repositorio

Línea de trabajo

El comportamiento reportado se gestiona en dist/diffable-html.js, en el callback onText; empieza leyendo ese handler y la lógica de formato que lo rodea. Reproduce la entrada multilínea proporcionada y verifica el tratamiento previsto de cada línea, incluido si las líneas vacías deben conservarse o eliminarse.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
javascript
Área
tooling
Tipo de issue
Error
Dificultad
2/5
Tiempo estimado
1-3 horas
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
48/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.