Skip to content

Commit

Permalink
refactor(core/reindent): move reindent to utils (#4629)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoscaceres authored Jan 6, 2024
1 parent 747cb20 commit f23bc70
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 24 deletions.
3 changes: 1 addition & 2 deletions src/core/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
*
*/

import { getElementIndentation } from "./utils.js";
import { getElementIndentation, reindent } from "./utils.js";
import { marked } from "./import-maps.js";
import { reindent } from "./reindent.js";
export const name = "core/markdown";

const gtEntity = />/gm;
Expand Down
22 changes: 3 additions & 19 deletions src/core/reindent.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,13 @@
// @ts-check
/**
* Module core/reindent
* @module core/reindent
*
* Removes common indents across the IDL texts,
* Normalizes indents across the pre elements in the document,
* so that indentation inside <pre> won't affect the rendered result.
*/

import { reindent } from "./utils.js";
export const name = "core/reindent";

/**
* @param {string} text
*/
export function reindent(text) {
if (!text) {
return text;
}
const lines = text.trimEnd().split("\n");
while (lines.length && !lines[0].trim()) {
lines.shift();
}
const indents = lines.filter(s => s.trim()).map(s => s.search(/[^\s]/));
const leastIndent = Math.min(...indents);
return lines.map(s => s.slice(leastIndent)).join("\n");
}

export function run() {
for (const pre of document.getElementsByTagName("pre")) {
pre.innerHTML = reindent(pre.innerHTML);
Expand Down
3 changes: 1 addition & 2 deletions src/core/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
// - make a release candidate that people can test
// - once we have something decent, merge, ship as 3.2.0
import { html, pluralize } from "./import-maps.js";
import { reindent, xmlEscape } from "./utils.js";
import css from "../styles/ui.css.js";
import { markdownToHtml } from "./markdown.js";
import { reindent } from "./reindent.js";
import { sub } from "./pubsubhub.js";
import { xmlEscape } from "./utils.js";
export const name = "core/ui";

// Opportunistically inserts the style, with the chance to reduce some FOUC
Expand Down
22 changes: 21 additions & 1 deletion src/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import { lang as docLang } from "./l10n.js";
import { html } from "./import-maps.js";
import { pub } from "./pubsubhub.js";
import { reindent } from "./reindent.js";
export const name = "core/utils";

const dashes = /-/g;
Expand Down Expand Up @@ -962,3 +961,24 @@ export function docLink(strings, ...keys) {
.join("");
return reindent(linkifiedStr);
}

/**
* Takes a text string, trims it, splits it into lines,
* finds the common indentation level, and then de-indents every line
* by that common indentation level.
*
* @param {string} text - The text to be re-indented.
* @returns {string} The re-indented text.
*/
export function reindent(text) {
if (!text) {
return text;
}
const lines = text.trimEnd().split("\n");
while (lines.length && !lines[0].trim()) {
lines.shift();
}
const indents = lines.filter(s => s.trim()).map(s => s.search(/[^\s]/));
const leastIndent = Math.min(...indents);
return lines.map(s => s.slice(leastIndent)).join("\n");
}

0 comments on commit f23bc70

Please sign in to comment.