Skip to content

Commit ff51626

Browse files
committed
2 parents 659393f + 01e3c4d commit ff51626

File tree

2 files changed

+44
-16
lines changed

2 files changed

+44
-16
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 4.2.1 - 01/02/2025
2+
3+
- Bug Fix(light-code): Make `dedent` from slotted tags not suck [#]
4+
15
## 4.2.0 - 11/04/2024
26

37
- Styles(light-code): Default line wrapping of `<light-code>` is now `"hard"`

internal/dedent.js

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1+
/**
2+
* This may be wrong, but we assume a `\t` is equivalent to 2 spaces.
3+
*/
4+
const TAB_LENGTH = 2;
5+
6+
const INDENT_REGEXP = new RegExp(`(\t| {${TAB_LENGTH}})`);
7+
18
/**
29
* @param {TemplateStringsArray|string} templateStrings
310
* @param {any[]} values
11+
* @returns {string}
412
*/
513
export function dedent(templateStrings, ...values) {
614
let matches = [];
@@ -9,40 +17,56 @@ export function dedent(templateStrings, ...values) {
917
? [templateStrings]
1018
: templateStrings.slice();
1119

12-
// 1. Remove trailing whitespace.
13-
strings[strings.length - 1] = strings[strings.length - 1].replace(
14-
/\r?\n([\t ]*)$/,
15-
"",
16-
);
20+
let string = "";
21+
22+
function interpolate() {
23+
string = strings[0];
24+
25+
for (let i = 0; i < values.length; i++) {
26+
string += values[i] + strings[i + 1];
27+
}
28+
29+
string = string.trim();
30+
}
31+
32+
// 1. check if its dedentable.
33+
let isDedentable = true;
1734

1835
// 2. Find all line breaks to determine the highest common indentation level.
1936
for (let i = 0; i < strings.length; i++) {
2037
let match;
2138

22-
if ((match = strings[i].match(/\n[\t ]+/g))) {
39+
// If any new line starts without any indentation and not an empty string, mark it as not dedentable, and then break the loop.
40+
if (strings[i].trim() && strings[i].match(/\n[^\t ]/)) {
41+
isDedentable = false;
42+
break;
43+
}
44+
45+
if (
46+
(match = strings[i].match(new RegExp(`\n${INDENT_REGEXP.source}+`, "g")))
47+
) {
2348
matches.push(...match);
2449
}
2550
}
2651

52+
if (!isDedentable) {
53+
interpolate();
54+
return string;
55+
}
56+
2757
// 3. Remove the common indentation from all strings.
2858
if (matches.length) {
2959
let size = Math.min(...matches.map((value) => value.length - 1));
30-
let pattern = new RegExp(`\n[\t ]{${size}}`, "g");
60+
let pattern = new RegExp(`\n(\t| ){${size}}`, "g");
3161

3262
for (let i = 0; i < strings.length; i++) {
33-
strings[i] = strings[i].replace(pattern, "\n");
63+
strings[i] = strings[i].replaceAll(pattern, "\n");
3464
}
3565
}
3666

37-
// 4. Remove leading whitespace.
38-
strings[0] = strings[0].replace(/^\r?\n/, "");
39-
4067
// 5. Perform interpolation.
41-
let string = strings[0];
42-
43-
for (let i = 0; i < values.length; i++) {
44-
string += values[i] + strings[i + 1];
45-
}
68+
interpolate();
4669

4770
return string;
4871
}
72+

0 commit comments

Comments
 (0)