algolia / algolia/diffable-html

Invalid multiline text node indentation

Open
#60 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
38
Forks
8
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

No contributing guide indexed for this repository

Research direction

The reported behavior is handled in dist/diffable-html.js, in the onText callback; start by reading that handler and its surrounding formatting logic. Reproduce the supplied multiline input and verify the intended treatment of each line, including whether empty lines should be preserved or removed.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.