Commit 02653a65 authored by Miguel Angel Reina Ortega's avatar Miguel Angel Reina Ortega
Browse files

Add a markdown linter custom rule to ignore grid tables

parent 362b469a
Loading
Loading
Loading
Loading
Loading
+1
Changes for .gitlab-ci.yml: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -104,6 +104,7 @@ Build markdownlint docker image:
      changes:
        - markdownTools/dockerfile.markdownlint
        - markdownTools/markdownlint-cli2.jsonc
        - markdownTools/onem2m-grid-table.mjs

Build Pandoc docker image:
  stage: build
+1
Changes for markdownTools/dockerfile.markdownlint: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -5,5 +5,6 @@ RUN npm install -g markdownlint-cli2@0.23.3 \
    && npm cache clean --force

COPY markdownTools/markdownlint-cli2.jsonc /opt/onem2m/markdownlint-cli2.jsonc
COPY markdownTools/onem2m-grid-table.mjs /opt/onem2m/onem2m-grid-table.mjs

WORKDIR /workdir
+4 −1
Changes for markdownTools/markdownlint-cli2.jsonc: 4 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -16,5 +16,8 @@
    "MD051": {
      "ignored_pattern": "^_ref_|^table|^figure"
    }
  }
  },
  "customRules": [
    "/opt/onem2m/onem2m-grid-table.mjs"
  ]
}
+107
Changes for markdownTools/onem2m-grid-table.mjs: 107 added lines, 0 removed lines.
Original line number Diff line number Diff line
// A grid table starts and ends with a "+----+---+" border. CommonMark treats a
// later "+    " row as a list, so the built-in rules report it. This rule finds
// each whole table and drops findings on those lines.

const dashBorder = /^\+(?:-+\+)+$/;
const tableLine = /^\|(?:.*\|)?\s*$|^\+[-=:+| ]*\+$/;

const originalPush = Array.prototype.push;
let pending = [];
let pendingTarget = null;

function isLintError(item) {
  return !!item &&
    typeof item === "object" &&
    Array.isArray(item.ruleNames) &&
    (item.ruleNames.length > 0) &&
    (typeof item.ruleNames[0] === "string") &&
    (typeof item.lineNumber === "number") &&
    (typeof item.ruleDescription === "string") &&
    ((item.errorContext === null) || (typeof item.errorContext === "string")) &&
    ((item.fixInfo === null) || (typeof item.fixInfo === "object")) &&
    !Object.prototype.hasOwnProperty.call(item, "fileName");
}

Array.prototype.push = function push(...items) {
  if ((items.length === 1) && isLintError(items[0])) {
    pendingTarget = this;
    originalPush.call(pending, items[0]);
    return this.length;
  }
  return originalPush.apply(this, items);
};

function isDashBorder(line) {
  return dashBorder.test(line.trim());
}

function isTableLine(line) {
  const text = line.trim();
  if (!tableLine.test(text) || !text.startsWith("+") && !text.startsWith("|")) {
    return false;
  }
  if (text.startsWith("|")) {
    return true;
  }
  const pluses = text.match(/\+/g);
  return !!pluses && (pluses.length >= 2);
}

function gridTableRanges(lines) {
  const ranges = [];
  for (let index = 0; index < lines.length; index++) {
    if (!isDashBorder(lines[index])) {
      continue;
    }
    let end = index + 1;
    while ((end < lines.length) && isTableLine(lines[end])) {
      end++;
    }
    const last = end - 1;
    if ((last > index) && isDashBorder(lines[last])) {
      ranges.push([ index + 1, last + 1 ]);
      index = last;
    }
  }
  return ranges;
}

function lineInRanges(lineNumber, ranges) {
  return ranges.some(([ start, end ]) => (lineNumber >= start) && (lineNumber <= end));
}

function flush(ranges) {
  const errors = pending;
  const target = pendingTarget;
  pending = [];
  pendingTarget = null;
  if (!target) {
    return;
  }
  for (const error of errors) {
    if (!lineInRanges(error.lineNumber, ranges)) {
      originalPush.call(target, error);
    }
  }
}

function onem2mGridTable(params) {
  const lines = [
    ...(params.frontMatterLines || []),
    ...(params.lines || [])
  ];
  let ranges = [];
  try {
    ranges = gridTableRanges(lines);
  } finally {
    flush(ranges);
  }
}

export default {
  "names": [ "ONEM2M001", "grid-table" ],
  "description": "Ignore findings inside grid tables",
  "tags": [ "onem2m" ],
  "parser": "none",
  "function": onem2mGridTable
};