Skip to content

Commit

Permalink
Improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
mohd-akram committed Dec 20, 2023
1 parent 667affc commit 14ac411
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,32 +90,35 @@ function format(/** @type {string} */ html, indent = " ", width = 80) {
let spanLevel = 0;
let lastSpace = "";

const flushOutput = () => {
if (lastSpace && lastSpace != "\n") {
const newline = span.indexOf("\n");
const len = newline == -1 ? span.length : newline;
if (lineLength + lastSpace.length + len > width) lastSpace = "\n";
}

const ind = lastSpace == "\n" && span ? indent.repeat(spanLevel) : "";
const out = `${lastSpace}${ind}${span}`;

if (out) {
const pos = out.lastIndexOf("\n");
if (pos == -1) lineLength += out.length;
else lineLength = out.length - pos - 1;
output.push(out);
}

span = lastSpace = "";
};

const addOutput = (/** @type {string[]} */ ...args) => {
for (const s of args) {
let out = "";

if (!s || (!specialElement && /^\s+$/.test(s))) {
const newline = span.indexOf("\n");
const len = newline == -1 ? span.length : newline;
if (lastSpace) {
if (lineLength + lastSpace.length + len > width) lastSpace = "\n";
out += lastSpace;
if (lastSpace == "\n" && span) out += indent.repeat(spanLevel);
}
out += span;
span = "";
if (!specialElement && /^\s+$/.test(s)) {
flushOutput();
lastSpace = s;
} else {
if (!span) spanLevel = level;
span += s;
}

if (out) {
const pos = out.lastIndexOf("\n");
if (pos == -1) lineLength += out.length;
else lineLength = out.length - pos - 1;
output.push(out);
}
}
};

Expand Down Expand Up @@ -207,7 +210,7 @@ function format(/** @type {string} */ html, indent = " ", width = 80) {
}

// Flush remaining output
addOutput("");
flushOutput();

let newline = false;
while (/^\s+$/.test(output[output.length - 1])) {
Expand Down

0 comments on commit 14ac411

Please sign in to comment.