markdown-table-prettify
Version:
Transforms markdown tables to be more readable.
107 lines (106 loc) • 5.01 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from) {
for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
to[j] = from[i];
return to;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FairTableIndentationDetector = exports.TableIndentationDetector = void 0;
var TableIndentationDetector = /** @class */ (function () {
function TableIndentationDetector() {
}
TableIndentationDetector.prototype.getLeftPad = function (lines) {
var leftPadsPerLine = lines.map(function (l) { return l.match(/^\s*/)[0]; });
return this.hasIndentation(leftPadsPerLine)
? this.getIndentationChars(leftPadsPerLine)
: "";
};
return TableIndentationDetector;
}());
exports.TableIndentationDetector = TableIndentationDetector;
/**
* If more than half of the lines have indentation, assume indentation was intended.
* Use the indentation characters used by the majority of the lines.
*/
var FairTableIndentationDetector = /** @class */ (function (_super) {
__extends(FairTableIndentationDetector, _super);
function FairTableIndentationDetector() {
return _super !== null && _super.apply(this, arguments) || this;
}
FairTableIndentationDetector.prototype.hasIndentation = function (leftPadsPerLine) {
var totalLines = leftPadsPerLine.length;
var linesWithActualLeftPadding = leftPadsPerLine.filter(function (p) { return p.length > 0; }).length;
return linesWithActualLeftPadding >= totalLines / 2;
};
FairTableIndentationDetector.prototype.getIndentationChars = function (leftPadsPerLine) {
var e_1, _a;
var nonEmptyLeftPads = leftPadsPerLine.filter(function (l) { return l.length > 0; });
var indentCounters = new Map();
try {
for (var nonEmptyLeftPads_1 = __values(nonEmptyLeftPads), nonEmptyLeftPads_1_1 = nonEmptyLeftPads_1.next(); !nonEmptyLeftPads_1_1.done; nonEmptyLeftPads_1_1 = nonEmptyLeftPads_1.next()) {
var leftPad = nonEmptyLeftPads_1_1.value;
var count = 1;
if (indentCounters.has(leftPad)) {
count += indentCounters.get(leftPad);
}
indentCounters.set(leftPad, ++count);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (nonEmptyLeftPads_1_1 && !nonEmptyLeftPads_1_1.done && (_a = nonEmptyLeftPads_1.return)) _a.call(nonEmptyLeftPads_1);
}
finally { if (e_1) throw e_1.error; }
}
// if there is an indentation used for at least 2 distinct lines, then use that, otherwise use the first line's indentation
var indentWithMostRepeats = __spreadArray([], __read(indentCounters.entries())).reduce(function (prev, curr) { return curr[1] > prev[1] ? curr : prev; });
return indentWithMostRepeats[1] > 1
? indentWithMostRepeats[0]
: indentCounters[0];
};
return FairTableIndentationDetector;
}(TableIndentationDetector));
exports.FairTableIndentationDetector = FairTableIndentationDetector;
;