Skip to content
This repository was archived by the owner on Jun 7, 2024. It is now read-only.

Commit f23bc70

Browse files
refactor(core/reindent): move reindent to utils (speced#4629)
1 parent 747cb20 commit f23bc70

File tree

4 files changed

+26
-24
lines changed

4 files changed

+26
-24
lines changed

src/core/markdown.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@
1111
*
1212
*/
1313

14-
import { getElementIndentation } from "./utils.js";
14+
import { getElementIndentation, reindent } from "./utils.js";
1515
import { marked } from "./import-maps.js";
16-
import { reindent } from "./reindent.js";
1716
export const name = "core/markdown";
1817

1918
const gtEntity = />/gm;

src/core/reindent.js

+3-19
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,13 @@
11
// @ts-check
22
/**
3-
* Module core/reindent
3+
* @module core/reindent
44
*
5-
* Removes common indents across the IDL texts,
5+
* Normalizes indents across the pre elements in the document,
66
* so that indentation inside <pre> won't affect the rendered result.
77
*/
8-
8+
import { reindent } from "./utils.js";
99
export const name = "core/reindent";
1010

11-
/**
12-
* @param {string} text
13-
*/
14-
export function reindent(text) {
15-
if (!text) {
16-
return text;
17-
}
18-
const lines = text.trimEnd().split("\n");
19-
while (lines.length && !lines[0].trim()) {
20-
lines.shift();
21-
}
22-
const indents = lines.filter(s => s.trim()).map(s => s.search(/[^\s]/));
23-
const leastIndent = Math.min(...indents);
24-
return lines.map(s => s.slice(leastIndent)).join("\n");
25-
}
26-
2711
export function run() {
2812
for (const pre of document.getElementsByTagName("pre")) {
2913
pre.innerHTML = reindent(pre.innerHTML);

src/core/ui.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@
99
// - make a release candidate that people can test
1010
// - once we have something decent, merge, ship as 3.2.0
1111
import { html, pluralize } from "./import-maps.js";
12+
import { reindent, xmlEscape } from "./utils.js";
1213
import css from "../styles/ui.css.js";
1314
import { markdownToHtml } from "./markdown.js";
14-
import { reindent } from "./reindent.js";
1515
import { sub } from "./pubsubhub.js";
16-
import { xmlEscape } from "./utils.js";
1716
export const name = "core/ui";
1817

1918
// Opportunistically inserts the style, with the chance to reduce some FOUC

src/core/utils.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import { lang as docLang } from "./l10n.js";
66
import { html } from "./import-maps.js";
77
import { pub } from "./pubsubhub.js";
8-
import { reindent } from "./reindent.js";
98
export const name = "core/utils";
109

1110
const dashes = /-/g;
@@ -962,3 +961,24 @@ export function docLink(strings, ...keys) {
962961
.join("");
963962
return reindent(linkifiedStr);
964963
}
964+
965+
/**
966+
* Takes a text string, trims it, splits it into lines,
967+
* finds the common indentation level, and then de-indents every line
968+
* by that common indentation level.
969+
*
970+
* @param {string} text - The text to be re-indented.
971+
* @returns {string} The re-indented text.
972+
*/
973+
export function reindent(text) {
974+
if (!text) {
975+
return text;
976+
}
977+
const lines = text.trimEnd().split("\n");
978+
while (lines.length && !lines[0].trim()) {
979+
lines.shift();
980+
}
981+
const indents = lines.filter(s => s.trim()).map(s => s.search(/[^\s]/));
982+
const leastIndent = Math.min(...indents);
983+
return lines.map(s => s.slice(leastIndent)).join("\n");
984+
}

0 commit comments

Comments
 (0)