Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidAnson committed Oct 21, 2024
1 parent effab8d commit 3e6005f
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 66 deletions.
44 changes: 28 additions & 16 deletions helpers/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,23 @@ module.exports.frontMatterHasTitle =
*/
function getReferenceLinkImageData(tokens) {
const normalizeReference = (s) => s.toLowerCase().trim().replace(/\s+/g, " ");
const references = new Map();
const shortcuts = new Map();
const addReferenceToDictionary = (token, label, isShortcut) => {
const referenceDatum = [
token.startLine - 1,
token.startColumn - 1,
token.text.length
];
const reference = normalizeReference(label);
const dictionary = isShortcut ? shortcuts : references;
const referenceData = dictionary.get(reference) || [];
referenceData.push(referenceDatum);
dictionary.set(reference, referenceData);
};
const definitions = new Map();
const definitionLineIndices = [];
const duplicateDefinitions = [];
const references = new Map();
const shortcuts = new Map();
const filteredTokens =
micromark.filterByTypes(
tokens,
Expand All @@ -383,7 +395,9 @@ function getReferenceLinkImageData(tokens) {
// definitions and definitionLineIndices
"definitionLabelString", "gfmFootnoteDefinitionLabelString",
// references and shortcuts
"gfmFootnoteCall", "image", "link"
"gfmFootnoteCall", "image", "link",
// undefined link labels
"undefinedReferenceCollapsed", "undefinedReferenceFull", "undefinedReferenceShortcut"

Check failure on line 400 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 20)

Type '"undefinedReferenceCollapsed"' is not assignable to type 'keyof TokenTypeMap'.

Check failure on line 400 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 20)

Type '"undefinedReferenceFull"' is not assignable to type 'keyof TokenTypeMap'.

Check failure on line 400 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 20)

Type '"undefinedReferenceShortcut"' is not assignable to type 'keyof TokenTypeMap'.

Check failure on line 400 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18)

Type '"undefinedReferenceCollapsed"' is not assignable to type 'keyof TokenTypeMap'.

Check failure on line 400 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18)

Type '"undefinedReferenceFull"' is not assignable to type 'keyof TokenTypeMap'.

Check failure on line 400 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18)

Type '"undefinedReferenceShortcut"' is not assignable to type 'keyof TokenTypeMap'.

Check failure on line 400 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (macos-latest, 18)

Type '"undefinedReferenceFull"' is not assignable to type 'keyof TokenTypeMap'.

Check failure on line 400 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (macos-latest, 18)

Type '"undefinedReferenceShortcut"' is not assignable to type 'keyof TokenTypeMap'.

Check failure on line 400 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (macos-latest, 20)

Type '"undefinedReferenceFull"' is not assignable to type 'keyof TokenTypeMap'.

Check failure on line 400 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (macos-latest, 20)

Type '"undefinedReferenceShortcut"' is not assignable to type 'keyof TokenTypeMap'.

Check failure on line 400 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 22)

Type '"undefinedReferenceCollapsed"' is not assignable to type 'keyof TokenTypeMap'.

Check failure on line 400 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 22)

Type '"undefinedReferenceFull"' is not assignable to type 'keyof TokenTypeMap'.

Check failure on line 400 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 22)

Type '"undefinedReferenceShortcut"' is not assignable to type 'keyof TokenTypeMap'.

Check failure on line 400 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (macos-latest, 22)

Type '"undefinedReferenceCollapsed"' is not assignable to type 'keyof TokenTypeMap'.

Check failure on line 400 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (macos-latest, 22)

Type '"undefinedReferenceFull"' is not assignable to type 'keyof TokenTypeMap'.

Check failure on line 400 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (macos-latest, 22)

Type '"undefinedReferenceShortcut"' is not assignable to type 'keyof TokenTypeMap'.

Check failure on line 400 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (windows-latest, 18)

Type '"undefinedReferenceCollapsed"' is not assignable to type 'keyof TokenTypeMap'.

Check failure on line 400 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (windows-latest, 18)

Type '"undefinedReferenceFull"' is not assignable to type 'keyof TokenTypeMap'.

Check failure on line 400 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (windows-latest, 18)

Type '"undefinedReferenceShortcut"' is not assignable to type 'keyof TokenTypeMap'.

Check failure on line 400 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (windows-latest, 22)

Type '"undefinedReferenceCollapsed"' is not assignable to type 'keyof TokenTypeMap'.

Check failure on line 400 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (windows-latest, 22)

Type '"undefinedReferenceFull"' is not assignable to type 'keyof TokenTypeMap'.

Check failure on line 400 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (windows-latest, 22)

Type '"undefinedReferenceShortcut"' is not assignable to type 'keyof TokenTypeMap'.

Check failure on line 400 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (windows-latest, 20)

Type '"undefinedReferenceCollapsed"' is not assignable to type 'keyof TokenTypeMap'.

Check failure on line 400 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (windows-latest, 20)

Type '"undefinedReferenceFull"' is not assignable to type 'keyof TokenTypeMap'.

Check failure on line 400 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (windows-latest, 20)

Type '"undefinedReferenceShortcut"' is not assignable to type 'keyof TokenTypeMap'.
]
);
for (const token of filteredTokens) {
Expand Down Expand Up @@ -439,22 +453,20 @@ function getReferenceLinkImageData(tokens) {
}
// Track link (handle shortcuts separately due to ambiguity in "text [text] text")
if (isShortcut || isFullOrCollapsed) {
const referenceDatum = [
token.startLine - 1,
token.startColumn - 1,
token.text.length,
label.length,
(referenceString?.text || "").length
];
const reference =
normalizeReference(referenceString?.text || label);
const dictionary = isShortcut ? shortcuts : references;
const referenceData = dictionary.get(reference) || [];
referenceData.push(referenceDatum);
dictionary.set(reference, referenceData);
addReferenceToDictionary(token, referenceString?.text || label, isShortcut);
}
}
break;
case "undefinedReferenceCollapsed":

Check failure on line 460 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 20)

Type '"undefinedReferenceCollapsed"' is not comparable to type 'keyof TokenTypeMap'.

Check failure on line 460 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18)

Type '"undefinedReferenceCollapsed"' is not comparable to type 'keyof TokenTypeMap'.

Check failure on line 460 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (macos-latest, 18)

Type '"undefinedReferenceCollapsed"' is not comparable to type 'keyof TokenTypeMap'.

Check failure on line 460 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (macos-latest, 20)

Type '"undefinedReferenceCollapsed"' is not comparable to type 'keyof TokenTypeMap'.

Check failure on line 460 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 22)

Type '"undefinedReferenceCollapsed"' is not comparable to type 'keyof TokenTypeMap'.

Check failure on line 460 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (macos-latest, 22)

Type '"undefinedReferenceCollapsed"' is not comparable to type 'keyof TokenTypeMap'.

Check failure on line 460 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (windows-latest, 18)

Type '"undefinedReferenceCollapsed"' is not comparable to type 'keyof TokenTypeMap'.

Check failure on line 460 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (windows-latest, 22)

Type '"undefinedReferenceCollapsed"' is not comparable to type 'keyof TokenTypeMap'.

Check failure on line 460 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (windows-latest, 20)

Type '"undefinedReferenceCollapsed"' is not comparable to type 'keyof TokenTypeMap'.
case "undefinedReferenceFull":

Check failure on line 461 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 20)

Type '"undefinedReferenceFull"' is not comparable to type 'keyof TokenTypeMap'.

Check failure on line 461 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18)

Type '"undefinedReferenceFull"' is not comparable to type 'keyof TokenTypeMap'.

Check failure on line 461 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (macos-latest, 18)

Type '"undefinedReferenceFull"' is not comparable to type 'keyof TokenTypeMap'.

Check failure on line 461 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (macos-latest, 20)

Type '"undefinedReferenceFull"' is not comparable to type 'keyof TokenTypeMap'.

Check failure on line 461 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 22)

Type '"undefinedReferenceFull"' is not comparable to type 'keyof TokenTypeMap'.

Check failure on line 461 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (macos-latest, 22)

Type '"undefinedReferenceFull"' is not comparable to type 'keyof TokenTypeMap'.

Check failure on line 461 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (windows-latest, 18)

Type '"undefinedReferenceFull"' is not comparable to type 'keyof TokenTypeMap'.

Check failure on line 461 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (windows-latest, 22)

Type '"undefinedReferenceFull"' is not comparable to type 'keyof TokenTypeMap'.

Check failure on line 461 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (windows-latest, 20)

Type '"undefinedReferenceFull"' is not comparable to type 'keyof TokenTypeMap'.
case "undefinedReferenceShortcut":

Check failure on line 462 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 20)

Type '"undefinedReferenceShortcut"' is not comparable to type 'keyof TokenTypeMap'.

Check failure on line 462 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18)

Type '"undefinedReferenceShortcut"' is not comparable to type 'keyof TokenTypeMap'.

Check failure on line 462 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (macos-latest, 18)

Type '"undefinedReferenceShortcut"' is not comparable to type 'keyof TokenTypeMap'.

Check failure on line 462 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (macos-latest, 20)

Type '"undefinedReferenceShortcut"' is not comparable to type 'keyof TokenTypeMap'.

Check failure on line 462 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 22)

Type '"undefinedReferenceShortcut"' is not comparable to type 'keyof TokenTypeMap'.

Check failure on line 462 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (macos-latest, 22)

Type '"undefinedReferenceShortcut"' is not comparable to type 'keyof TokenTypeMap'.

Check failure on line 462 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (windows-latest, 18)

Type '"undefinedReferenceShortcut"' is not comparable to type 'keyof TokenTypeMap'.

Check failure on line 462 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (windows-latest, 22)

Type '"undefinedReferenceShortcut"' is not comparable to type 'keyof TokenTypeMap'.

Check failure on line 462 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (windows-latest, 20)

Type '"undefinedReferenceShortcut"' is not comparable to type 'keyof TokenTypeMap'.
{
const undefinedReference = micromark.getDescendantsByType(token, [ "undefinedReference" ])[0];

Check failure on line 464 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 20)

Type '"undefinedReference"' is not assignable to type 'keyof TokenTypeMap | (keyof TokenTypeMap)[]'.

Check failure on line 464 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18)

Type '"undefinedReference"' is not assignable to type 'keyof TokenTypeMap | (keyof TokenTypeMap)[]'.

Check failure on line 464 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (macos-latest, 18)

Type '"undefinedReference"' is not assignable to type 'keyof TokenTypeMap | (keyof TokenTypeMap)[]'.

Check failure on line 464 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (macos-latest, 20)

Type '"undefinedReference"' is not assignable to type 'keyof TokenTypeMap | (keyof TokenTypeMap)[]'.

Check failure on line 464 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 22)

Type '"undefinedReference"' is not assignable to type 'keyof TokenTypeMap | (keyof TokenTypeMap)[]'.

Check failure on line 464 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (macos-latest, 22)

Type '"undefinedReference"' is not assignable to type 'keyof TokenTypeMap | (keyof TokenTypeMap)[]'.

Check failure on line 464 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (windows-latest, 18)

Type '"undefinedReference"' is not assignable to type 'keyof TokenTypeMap | (keyof TokenTypeMap)[]'.

Check failure on line 464 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (windows-latest, 22)

Type '"undefinedReference"' is not assignable to type 'keyof TokenTypeMap | (keyof TokenTypeMap)[]'.

Check failure on line 464 in helpers/helpers.js

View workflow job for this annotation

GitHub Actions / build (windows-latest, 20)

Type '"undefinedReference"' is not assignable to type 'keyof TokenTypeMap | (keyof TokenTypeMap)[]'.
const label = undefinedReference.children.map((t) => t.text).join("");
const isShortcut = (token.type === "undefinedReferenceShortcut");
addReferenceToDictionary(token, label, isShortcut);
}
break;
}
}
return {
Expand Down
150 changes: 133 additions & 17 deletions helpers/micromark-parse.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,32 @@ const micromark = require("markdownlint-micromark");
const { isHtmlFlowComment } = require("./micromark-helpers.cjs");
const { flatTokensSymbol, htmlFlowSymbol, newLineRe } = require("./shared.js");

/** @typedef {import("markdownlint-micromark").Construct} Construct */

Check failure on line 9 in helpers/micromark-parse.cjs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 20)

Namespace '"/home/runner/work/markdownlint/markdownlint/node_modules/markdownlint-micromark/micromark"' has no exported member 'Construct'.

Check failure on line 9 in helpers/micromark-parse.cjs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18)

Namespace '"/home/runner/work/markdownlint/markdownlint/node_modules/markdownlint-micromark/micromark"' has no exported member 'Construct'.

Check failure on line 9 in helpers/micromark-parse.cjs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 18)

Namespace '"/Users/runner/work/markdownlint/markdownlint/node_modules/markdownlint-micromark/micromark"' has no exported member 'Construct'.

Check failure on line 9 in helpers/micromark-parse.cjs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 20)

Namespace '"/Users/runner/work/markdownlint/markdownlint/node_modules/markdownlint-micromark/micromark"' has no exported member 'Construct'.

Check failure on line 9 in helpers/micromark-parse.cjs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 22)

Namespace '"/home/runner/work/markdownlint/markdownlint/node_modules/markdownlint-micromark/micromark"' has no exported member 'Construct'.

Check failure on line 9 in helpers/micromark-parse.cjs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 22)

Namespace '"/Users/runner/work/markdownlint/markdownlint/node_modules/markdownlint-micromark/micromark"' has no exported member 'Construct'.

Check failure on line 9 in helpers/micromark-parse.cjs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 18)

Namespace '"D:/a/markdownlint/markdownlint/node_modules/markdownlint-micromark/micromark"' has no exported member 'Construct'.

Check failure on line 9 in helpers/micromark-parse.cjs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 22)

Namespace '"D:/a/markdownlint/markdownlint/node_modules/markdownlint-micromark/micromark"' has no exported member 'Construct'.

Check failure on line 9 in helpers/micromark-parse.cjs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 20)

Namespace '"D:/a/markdownlint/markdownlint/node_modules/markdownlint-micromark/micromark"' has no exported member 'Construct'.
/** @typedef {import("markdownlint-micromark").Event} Event */
/** @typedef {import("markdownlint-micromark").ParseOptions} MicromarkParseOptions */
/** @typedef {import("markdownlint-micromark").State} State */

Check failure on line 12 in helpers/micromark-parse.cjs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 20)

Namespace '"/home/runner/work/markdownlint/markdownlint/node_modules/markdownlint-micromark/micromark"' has no exported member 'State'.

Check failure on line 12 in helpers/micromark-parse.cjs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18)

Namespace '"/home/runner/work/markdownlint/markdownlint/node_modules/markdownlint-micromark/micromark"' has no exported member 'State'.

Check failure on line 12 in helpers/micromark-parse.cjs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 18)

Namespace '"/Users/runner/work/markdownlint/markdownlint/node_modules/markdownlint-micromark/micromark"' has no exported member 'State'.

Check failure on line 12 in helpers/micromark-parse.cjs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 20)

Namespace '"/Users/runner/work/markdownlint/markdownlint/node_modules/markdownlint-micromark/micromark"' has no exported member 'State'.

Check failure on line 12 in helpers/micromark-parse.cjs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 22)

Namespace '"/home/runner/work/markdownlint/markdownlint/node_modules/markdownlint-micromark/micromark"' has no exported member 'State'.

Check failure on line 12 in helpers/micromark-parse.cjs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 22)

Namespace '"/Users/runner/work/markdownlint/markdownlint/node_modules/markdownlint-micromark/micromark"' has no exported member 'State'.

Check failure on line 12 in helpers/micromark-parse.cjs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 18)

Namespace '"D:/a/markdownlint/markdownlint/node_modules/markdownlint-micromark/micromark"' has no exported member 'State'.

Check failure on line 12 in helpers/micromark-parse.cjs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 22)

Namespace '"D:/a/markdownlint/markdownlint/node_modules/markdownlint-micromark/micromark"' has no exported member 'State'.

Check failure on line 12 in helpers/micromark-parse.cjs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 20)

Namespace '"D:/a/markdownlint/markdownlint/node_modules/markdownlint-micromark/micromark"' has no exported member 'State'.
/** @typedef {import("markdownlint-micromark").Token} Token */
/** @typedef {import("markdownlint-micromark").Tokenizer} Tokenizer */

Check failure on line 14 in helpers/micromark-parse.cjs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 20)

Namespace '"/home/runner/work/markdownlint/markdownlint/node_modules/markdownlint-micromark/micromark"' has no exported member 'Tokenizer'.

Check failure on line 14 in helpers/micromark-parse.cjs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 18)

Namespace '"/home/runner/work/markdownlint/markdownlint/node_modules/markdownlint-micromark/micromark"' has no exported member 'Tokenizer'.

Check failure on line 14 in helpers/micromark-parse.cjs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 18)

Namespace '"/Users/runner/work/markdownlint/markdownlint/node_modules/markdownlint-micromark/micromark"' has no exported member 'Tokenizer'.

Check failure on line 14 in helpers/micromark-parse.cjs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 20)

Namespace '"/Users/runner/work/markdownlint/markdownlint/node_modules/markdownlint-micromark/micromark"' has no exported member 'Tokenizer'.

Check failure on line 14 in helpers/micromark-parse.cjs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, 22)

Namespace '"/home/runner/work/markdownlint/markdownlint/node_modules/markdownlint-micromark/micromark"' has no exported member 'Tokenizer'.

Check failure on line 14 in helpers/micromark-parse.cjs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 22)

Namespace '"/Users/runner/work/markdownlint/markdownlint/node_modules/markdownlint-micromark/micromark"' has no exported member 'Tokenizer'.

Check failure on line 14 in helpers/micromark-parse.cjs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 18)

Namespace '"D:/a/markdownlint/markdownlint/node_modules/markdownlint-micromark/micromark"' has no exported member 'Tokenizer'.

Check failure on line 14 in helpers/micromark-parse.cjs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 22)

Namespace '"D:/a/markdownlint/markdownlint/node_modules/markdownlint-micromark/micromark"' has no exported member 'Tokenizer'.

Check failure on line 14 in helpers/micromark-parse.cjs

View workflow job for this annotation

GitHub Actions / build (windows-latest, 20)

Namespace '"D:/a/markdownlint/markdownlint/node_modules/markdownlint-micromark/micromark"' has no exported member 'Tokenizer'.
/** @typedef {import("../lib/markdownlint.js").MicromarkToken} MicromarkToken */

/**
* Parse options.
*
* @typedef {Object} ParseOptions
* @property {boolean} [freezeTokens] Whether to freeze output Tokens.
* @property {boolean} [shimReferences] Whether to shim missing references.
*/

/**
* Parses a Markdown document and returns Micromark events.
*
* @param {string} markdown Markdown document.
* @param {ParseOptions} [parseOptions] Options.
* @param {MicromarkParseOptions} [micromarkParseOptions] Options for micromark.
* @returns {Event[]} Micromark events.
*/
function getEvents(
markdown,
parseOptions = {},
micromarkParseOptions = {}
) {
// Get options
const shimReferences = Boolean(parseOptions.shimReferences);

// Customize options object to add useful extensions
micromarkParseOptions.extensions = micromarkParseOptions.extensions || [];
micromarkParseOptions.extensions.push(
Expand All @@ -44,17 +42,135 @@ function getEvents(
micromark.math()
);

// Use micromark to parse document into Events
const encoding = undefined;
const eol = true;
const parseContext = micromark.parse(micromarkParseOptions);
if (shimReferences) {
// Customize ParseContext to treat all references as defined
parseContext.defined.includes = (searchElement) => searchElement.length > 0;
// // Shim labelEnd to identify undefined link labels
/** @type {Event[][]} */
const artificialEventLists = [];
/** @type {Construct} */
const labelEnd =
// @ts-ignore
micromark.labelEnd;
const tokenizeOriginal = labelEnd.tokenize;

/** @type {Tokenizer} */

Check failure on line 54 in helpers/micromark-parse.cjs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 18)

The type of a function declaration must match the function's signature.

Check failure on line 54 in helpers/micromark-parse.cjs

View workflow job for this annotation

GitHub Actions / build (macos-latest, 20)

The type of a function declaration must match the function's signature.
function tokenizeShim(effects, okOriginal, nokOriginal) {
// eslint-disable-next-line consistent-this, unicorn/no-this-assignment, no-invalid-this
const tokenizeContext = this;

/** @type {State} */
const nokShim = (code) => {
const events = tokenizeContext.events;

// Find start of label (image or link)
let indexStart = events.length;
while (--indexStart >= 0) {
const event = events[indexStart];
const [ kind, token ] = event;
if (kind === "enter") {
const { type } = token;
if ((type === "labelImage") || (type === "labelLink")) {
// Found it
break;
}
}
}

// If found...
if (indexStart >= 0) {
// Create artificial enter/exit events and replicate all data/lineEnding events within
const eventStart = events[indexStart];
const [ , eventStartToken ] = eventStart;
const eventEnd = events[events.length - 1];
const [ , eventEndToken ] = eventEnd;
/** @type {Token} */
const undefinedReferenceType = {
"type": "undefinedReferenceShortcut",
"start": eventStartToken.start,
"end": eventEndToken.end
};
/** @type {Token} */
const undefinedReference = {
"type": "undefinedReference",
"start": eventStartToken.start,
"end": eventEndToken.end
};
const eventsToReplicate = events.slice(indexStart)
.filter((event) => {
const [ , eventToken ] = event;
return (eventToken.type === "data") || (eventToken.type === "lineEnding");
});

// Determine the type of the undefined reference
const previousUndefinedEvent = (artificialEventLists.length > 0) && artificialEventLists[artificialEventLists.length - 1][0];
const previousUndefinedToken = previousUndefinedEvent && previousUndefinedEvent[1];
if (
previousUndefinedToken &&
(previousUndefinedToken.end.line === undefinedReferenceType.start.line) &&
(previousUndefinedToken.end.column === undefinedReferenceType.start.column)
) {
// Previous undefined reference event is immediately before this one
if (eventsToReplicate.length === 0) {
// The pair represent a collapsed reference (ex: [...][])
previousUndefinedToken.type = "undefinedReferenceCollapsed";
previousUndefinedToken.end = eventEndToken.end;
} else {
// The pair represent a full reference (ex: [...][...])
undefinedReferenceType.type = "undefinedReferenceFull";
undefinedReferenceType.start = previousUndefinedToken.start;
}
}

// Create artificial event list and replicate content
const text = eventsToReplicate
.filter((event) => event[0] === "enter")
.map((event) => tokenizeContext.sliceSerialize(event[1]))
.join("")
.trim();
if ((text.length > 0) && !text.includes("]")) {
/** @type {Event[]} */
const artificialEvents = [];
artificialEvents.push(
[ "enter", undefinedReferenceType, tokenizeContext ],
[ "enter", undefinedReference, tokenizeContext ]
);
for (const event of eventsToReplicate) {
const [ kind, token ] = event;
// Copy token because the current object will get modified by the parser
artificialEvents.push([ kind, { ...token }, tokenizeContext ]);
}
artificialEvents.push(
[ "exit", undefinedReference, tokenizeContext ],
[ "exit", undefinedReferenceType, tokenizeContext ]
);
artificialEventLists.push(artificialEvents);
}
}

// Continue with original behavior
return nokOriginal(code);
};

// Shim nok handler of labelEnd's tokenize
return tokenizeOriginal.call(tokenizeContext, effects, okOriginal, nokShim);
}

try {
// Shim labelEnd behavior to detect undefined references
labelEnd.tokenize = tokenizeShim;

// Use micromark to parse document into Events
const encoding = undefined;
const eol = true;
const parseContext = micromark.parse(micromarkParseOptions);
const chunks = micromark.preprocess()(markdown, encoding, eol);
const events = micromark.postprocess(parseContext.document().write(chunks));

// Append artificial events and return all events
// eslint-disable-next-line unicorn/prefer-spread
return events.concat(...artificialEventLists);
} finally {
// Restore shimmed labelEnd behavior
labelEnd.tokenize = tokenizeOriginal;
}
const chunks = micromark.preprocess()(markdown, encoding, eol);
const events = micromark.postprocess(parseContext.document().write(chunks));
return events;
}

/**
Expand All @@ -78,7 +194,7 @@ function parseInternal(
const freezeTokens = Boolean(parseOptions.freezeTokens);

// Use micromark to parse document into Events
const events = getEvents(markdown, parseOptions, micromarkParseOptions);
const events = getEvents(markdown, micromarkParseOptions);

// Create Token objects
const document = [];
Expand Down
2 changes: 1 addition & 1 deletion lib/markdownlint.js
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ function lintContent(
// Parse content into parser tokens
const micromarkTokens = micromark.parse(
content,
{ "freezeTokens": customRulesPresent, "shimReferences": true }
{ "freezeTokens": customRulesPresent }
);
// Hide the content of HTML comments from rules
const preClearedContent = content;
Expand Down
47 changes: 20 additions & 27 deletions lib/md034.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

const { addErrorContext } = require("../helpers");
const { filterByPredicate, getHtmlTagInfo, inHtmlFlow } = require("../helpers/micromark-helpers.cjs");
const { parse } = require("../helpers/micromark-parse.cjs");
const { filterByTypesCached } = require("./cache");

// eslint-disable-next-line jsdoc/valid-types
/** @type import("./markdownlint").Rule */
Expand Down Expand Up @@ -39,6 +37,7 @@ module.exports = {
return false;
},
(token) => {
// Ignore content of inline HTML tags
const { children } = token;
const result = [];
for (let i = 0; i < children.length; i++) {
Expand Down Expand Up @@ -69,31 +68,25 @@ module.exports = {
}
)
);
const autoLinks = filterByTypesCached([ "literalAutolink" ]);
if (autoLinks.length > 0) {
// Re-parse with correct link/image reference definition handling
const document = params.lines.join("\n");
const tokens = parse(document);
for (const token of literalAutolinks(tokens)) {
const range = [
token.startColumn,
token.endColumn - token.startColumn
];
const fixInfo = {
"editColumn": range[0],
"deleteCount": range[1],
"insertText": `<${token.text}>`
};
addErrorContext(
onError,
token.startLine,
token.text,
undefined,
undefined,
range,
fixInfo
);
}
for (const token of literalAutolinks(params.parsers.micromark.tokens)) {
const range = [
token.startColumn,
token.endColumn - token.startColumn
];
const fixInfo = {
"editColumn": range[0],
"deleteCount": range[1],
"insertText": `<${token.text}>`
};
addErrorContext(
onError,
token.startLine,
token.text,
undefined,
undefined,
range,
fixInfo
);
}
}
};
1 change: 1 addition & 0 deletions micromark/exports.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export { math } from "micromark-extension-math";
export { parse } from "micromark";
export { postprocess } from "micromark";
export { preprocess } from "micromark";
export { labelEnd } from "micromark-core-commonmark";
15 changes: 12 additions & 3 deletions micromark/micromark.d.cts
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ declare type CompileOptions_2 = CompileOptions
/**
* An object describing how to parse a markdown construct.
*/
declare type Construct = {
export declare type Construct = {
/**
* Set up a state machine to handle character codes streaming in.
*/
Expand Down Expand Up @@ -1200,7 +1200,7 @@ context: TokenizeContext
* @returns
* Next state.
*/
declare type State = (code: Code) => State | undefined
export declare type State = (code: Code) => State | undefined

/**
* A token: a span of chunks.
Expand Down Expand Up @@ -1490,7 +1490,7 @@ declare interface TokenizeContext {
* @returns
* First state.
*/
declare type Tokenizer = (
export declare type Tokenizer = (
this: TokenizeContext,
effects: Effects,
ok: State,
Expand Down Expand Up @@ -1747,3 +1747,12 @@ export declare interface TokenTypeMap {
mathTextPadding: 'mathTextPadding'
mathTextSequence: 'mathTextSequence'
}

// Source: [CUSTOM]

export declare interface TokenTypeMap {
undefinedReference: 'undefinedReference'
undefinedReferenceCollapsed: 'undefinedReferenceCollapsed'
undefinedReferenceFull: 'undefinedReferenceFull'
undefinedReferenceShortcut: 'undefinedReferenceShortcut'
}
2 changes: 1 addition & 1 deletion micromark/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "markdownlint-micromark",
"version": "0.1.11",
"version": "0.1.12",
"description": "A trivial package that re-exports some micromark functionality as a CommonJS module",
"type": "commonjs",
"exports": "./micromark.cjs",
Expand Down
Loading

0 comments on commit 3e6005f

Please sign in to comment.