mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-19 23:40:12 +01:00
wip
This commit is contained in:
parent
7668b0a263
commit
f44a15e430
17 changed files with 333 additions and 27 deletions
|
|
@ -4,8 +4,46 @@ import { filterByTypes } from "../helpers/micromark-helpers.cjs";
|
|||
import { filterByTypesCached } from "./cache.mjs";
|
||||
|
||||
/** @typedef {import("micromark-extension-gfm-table")} */
|
||||
/** @typedef {import("markdownlint").MicromarkToken} MicromarkToken */
|
||||
/** @typedef {import("markdownlint").RuleOnErrorInfo} RuleOnErrorInfo */
|
||||
|
||||
// See https://unicode.org/reports/tr51/
|
||||
const defaultWideCharacterReString = "\\p{RGI_Emoji}";
|
||||
|
||||
/**
|
||||
* @typedef Column
|
||||
* @property {number} actual Actual column (1-based)
|
||||
* @property {number} effective Effective column (1-based)
|
||||
*/
|
||||
|
||||
/**
|
||||
* Gets the effective (adjusted for wide characters) column for an actual column.
|
||||
*
|
||||
* @param {string} line Line of text.
|
||||
* @param {number} column Actual column (1-based).
|
||||
* @param {RegExp} wideRe Wide character RegExp.
|
||||
* @returns {number} Effective column (1-based).
|
||||
*/
|
||||
function effectiveColumn(line, column, wideRe) {
|
||||
const wideCharacterCount = (line.slice(0, column - 1).match(wideRe) || []).length;
|
||||
return column + wideCharacterCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a list of table cell divider columns.
|
||||
*
|
||||
* @param {readonly string[]} lines File/string lines.
|
||||
* @param {MicromarkToken} row Micromark row token.
|
||||
* @param {RegExp} wideRe Wide character RegExp.
|
||||
* @returns {Column[]} Divider columns.
|
||||
*/
|
||||
function getTableDividerColumns(lines, row, wideRe) {
|
||||
return filterByTypes(
|
||||
row.children,
|
||||
[ "tableCellDivider" ]).map((divider) => ({ "actual": divider.startColumn, "effective": effectiveColumn(lines[row.startLine - 1], divider.startColumn, wideRe) })
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a RuleOnErrorInfo object to a list of RuleOnErrorInfo objects.
|
||||
*
|
||||
|
|
@ -33,6 +71,9 @@ export default {
|
|||
const styleAlignedAllowed = (style === "any") || (style === "aligned");
|
||||
const styleCompactAllowed = (style === "any") || (style === "compact");
|
||||
const styleTightAllowed = (style === "any") || (style === "tight");
|
||||
const wideCharacter = params.config.wide_character;
|
||||
const wideCharacterReString = (wideCharacter === undefined) ? defaultWideCharacterReString : wideCharacter;
|
||||
const wideCharacterRe = new RegExp(wideCharacterReString, "gv");
|
||||
|
||||
// Scan all tables/rows
|
||||
const tables = filterByTypesCached([ "table" ]);
|
||||
|
|
@ -44,13 +85,13 @@ export default {
|
|||
/** @type {RuleOnErrorInfo[]} */
|
||||
const errorsIfAligned = [];
|
||||
if (styleAlignedAllowed) {
|
||||
const headingDividerColumns = filterByTypes(headingRow.children, [ "tableCellDivider" ]).map((divider) => divider.startColumn);
|
||||
const headingDividerColumns = getTableDividerColumns(params.lines, headingRow, wideCharacterRe);
|
||||
for (const row of rows.slice(1)) {
|
||||
const remainingHeadingDividerColumns = new Set(headingDividerColumns);
|
||||
const rowDividerColumns = filterByTypes(row.children, [ "tableCellDivider" ]).map((divider) => divider.startColumn);
|
||||
const remainingHeadingDividerColumns = new Set(headingDividerColumns.map((column) => column.effective));
|
||||
const rowDividerColumns = getTableDividerColumns(params.lines, row, wideCharacterRe);
|
||||
for (const dividerColumn of rowDividerColumns) {
|
||||
if ((remainingHeadingDividerColumns.size > 0) && !remainingHeadingDividerColumns.delete(dividerColumn)) {
|
||||
addError(errorsIfAligned, row.startLine, dividerColumn, "Table pipe does not align with heading for style \"aligned\"");
|
||||
if ((remainingHeadingDividerColumns.size > 0) && !remainingHeadingDividerColumns.delete(dividerColumn.effective)) {
|
||||
addError(errorsIfAligned, row.startLine, dividerColumn.actual, "Table pipe does not align with heading for style \"aligned\"");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue