algolia / algolia/diffable-html

Invalid multiline text node indentation

Aperta
#60 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
JavaScript
Stelle
38
Fork
8
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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.

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Direzione di ricerca

Il comportamento segnalato viene gestito in dist/diffable-html.js, nel callback onText; inizia leggendo quell’handler e la logica di formattazione che lo circonda. Riproduci l’input multilinea fornito e verifica il trattamento previsto di ogni riga, incluso se le righe vuote debbano essere preservate o rimosse.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
javascript
Ambito
tooling
Tipo di issue
Bug
Difficoltà
2/5
Tempo stimato
1-3 ore
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
48/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.