UNPKG

markdown-table-prettify

Version:

Transforms markdown tables to be more readable.

66 lines (65 loc) 2.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TableFinder = void 0; var range_1 = require("../models/doc/range"); var TableFinder = /** @class */ (function () { function TableFinder(_tableValidator) { this._tableValidator = _tableValidator; this._ignoreStart = "<!-- markdown-table-prettify-ignore-start -->"; this._ignoreEnd = "<!-- markdown-table-prettify-ignore-end -->"; } TableFinder.prototype.getNextRange = function (document, startLine) { // look for the separator row, assume table starts 1 row before & ends when invalid var rowIndex = startLine; var isInIgnoreBlock = false; while (rowIndex < document.lines.length) { if (document.lines[rowIndex].value.trim() == this._ignoreStart) { isInIgnoreBlock = true; } else if (document.lines[rowIndex].value.trim() == this._ignoreEnd) { isInIgnoreBlock = false; } if (!isInIgnoreBlock) { var isValidSeparatorRow = this._tableValidator.lineIsValidSeparator(document.lines[rowIndex].value); var nextRangeResult = isValidSeparatorRow ? this.getNextValidTableRange(document, rowIndex) : { range: null, ignoreBlockStarted: isInIgnoreBlock }; isInIgnoreBlock = nextRangeResult.ignoreBlockStarted; if (nextRangeResult.range != null) { return nextRangeResult.range; } } rowIndex++; } return null; }; TableFinder.prototype.getNextValidTableRange = function (document, separatorRowIndex) { var firstTableFileRow = separatorRowIndex - 1; var lastTableFileRow = separatorRowIndex + 1; var selection = null; var ignoreBlockedStarted = false; while (lastTableFileRow < document.lines.length) { // when the ignore-start is in the middle of a possible table don't go further if (document.lines[lastTableFileRow].value.trim() == this._ignoreStart) { ignoreBlockedStarted = true; break; } var newSelection = document.getText(new range_1.Range(firstTableFileRow, lastTableFileRow)); var tableValid = this._tableValidator.isValid(newSelection); if (tableValid) { selection = newSelection; lastTableFileRow++; } else { break; } } // return the row to the last valid try return { range: selection != null ? new range_1.Range(firstTableFileRow, lastTableFileRow - 1) : null, ignoreBlockStarted: ignoreBlockedStarted }; }; return TableFinder; }()); exports.TableFinder = TableFinder;