Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions src/rules/no-duplicate-definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// Imports
//-----------------------------------------------------------------------------

import { normalizeIdentifier } from "micromark-util-normalize-identifier";
import { normalizeIdentifier } from "../util.js";

//-----------------------------------------------------------------------------
// Type Definitions
Expand Down Expand Up @@ -81,13 +81,11 @@ export default /** @satisfies {NoDuplicateDefinitionsRuleDefinition} */ ({

create(context) {
const allowDefinitions = new Set(
context.options[0].allowDefinitions.map(identifier =>
normalizeIdentifier(identifier).toLowerCase(),
),
context.options[0].allowDefinitions.map(normalizeIdentifier),
);
const allowFootnoteDefinitions = new Set(
context.options[0].allowFootnoteDefinitions.map(identifier =>
normalizeIdentifier(identifier).toLowerCase(),
context.options[0].allowFootnoteDefinitions.map(
normalizeIdentifier,
),
);
const [{ checkFootnoteDefinitions }] = context.options;
Expand Down
11 changes: 4 additions & 7 deletions src/rules/no-empty-definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
// Imports
//-----------------------------------------------------------------------------

import { normalizeIdentifier } from "micromark-util-normalize-identifier";
import { htmlCommentPattern } from "../util.js";
import { htmlCommentPattern, normalizeIdentifier } from "../util.js";

//-----------------------------------------------------------------------------
// Type Definitions
Expand Down Expand Up @@ -96,13 +95,11 @@ export default /** @satisfies {NoEmptyDefinitionsRuleDefinition} */ ({

create(context) {
const allowDefinitions = new Set(
context.options[0].allowDefinitions.map(identifier =>
normalizeIdentifier(identifier).toLowerCase(),
),
context.options[0].allowDefinitions.map(normalizeIdentifier),
);
const allowFootnoteDefinitions = new Set(
context.options[0].allowFootnoteDefinitions.map(identifier =>
normalizeIdentifier(identifier).toLowerCase(),
context.options[0].allowFootnoteDefinitions.map(
normalizeIdentifier,
),
);
const [{ checkFootnoteDefinitions }] = context.options;
Expand Down
5 changes: 2 additions & 3 deletions src/rules/no-reference-like-urls.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// Imports
//-----------------------------------------------------------------------------

import { normalizeIdentifier } from "micromark-util-normalize-identifier";
import { normalizeIdentifier } from "../util.js";

//-----------------------------------------------------------------------------
// Type Definitions
Expand Down Expand Up @@ -79,8 +79,7 @@ export default /** @satisfies {NoReferenceLikeUrlsRuleDefinition} */ ({
const { label, destination } = match.groups;
const { type, title } = node;
const prefix = type === "image" ? "!" : "";
const url =
normalizeIdentifier(destination).toLowerCase();
const url = normalizeIdentifier(destination);

if (definitionIdentifiers.has(url)) {
context.report({
Expand Down
10 changes: 4 additions & 6 deletions src/rules/no-unused-definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// Imports
//-----------------------------------------------------------------------------

import { normalizeIdentifier } from "micromark-util-normalize-identifier";
import { normalizeIdentifier } from "../util.js";

//-----------------------------------------------------------------------------
// Type Definitions
Expand Down Expand Up @@ -81,13 +81,11 @@ export default /** @satisfies {NoUnusedDefinitionsRuleDefinition} */ ({

create(context) {
const allowDefinitions = new Set(
context.options[0].allowDefinitions.map(identifier =>
normalizeIdentifier(identifier).toLowerCase(),
),
context.options[0].allowDefinitions.map(normalizeIdentifier),
);
const allowFootnoteDefinitions = new Set(
context.options[0].allowFootnoteDefinitions.map(identifier =>
normalizeIdentifier(identifier).toLowerCase(),
context.options[0].allowFootnoteDefinitions.map(
normalizeIdentifier,
),
);
const [{ checkFootnoteDefinitions }] = context.options;
Expand Down
15 changes: 15 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
* @author Nicholas C. Zakas
*/

//------------------------------------------------------------------------------
// Imports
//------------------------------------------------------------------------------

import { normalizeIdentifier as micromarkUtilNormalizeIdentifier } from "micromark-util-normalize-identifier";

//-----------------------------------------------------------------------------
// Regex Patterns
//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -48,6 +54,15 @@ export function frontmatterHasTitle(value, pattern) {
return false;
}

/**
* Normalizes a Markdown reference identifier.
* @param {string} identifier The identifier to normalize.
* @returns {string} The normalized lowercase identifier.
*/
export function normalizeIdentifier(identifier) {
return micromarkUtilNormalizeIdentifier(identifier).toLowerCase();
}

/**
* Replaces all HTML comments with whitespace.
* This preserves offsets and locations of characters
Expand Down
50 changes: 49 additions & 1 deletion tests/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
//------------------------------------------------------------------------------

import assert from "node:assert";
import { frontmatterHasTitle, stripHtmlComments } from "../src/util.js";
import {
frontmatterHasTitle,
normalizeIdentifier,
stripHtmlComments,
} from "../src/util.js";

//------------------------------------------------------------------------------
// Tests
Expand Down Expand Up @@ -72,6 +76,50 @@ describe("util", () => {
});
});

describe("normalizeIdentifier()", () => {
Comment thread
lumirlumir marked this conversation as resolved.
it("should preserve an already normalized identifier", () => {
const input = "example";
const result = normalizeIdentifier(input);
assert.strictEqual(result, "example");
});

it("should convert a mixed-case identifier to lowercase", () => {
const input = "ExAmPlE";
const result = normalizeIdentifier(input);
assert.strictEqual(result, "example");
});

it("should trim leading and trailing spaces", () => {
const input = " example ";
const result = normalizeIdentifier(input);
assert.strictEqual(result, "example");
});

it("should collapse consecutive internal spaces into a single space", () => {
const input = "foo bar";
const result = normalizeIdentifier(input);
assert.strictEqual(result, "foo bar");
});

it("should normalize tabs and line breaks to a single space", () => {
const input = "foo\t\r\nbar";
const result = normalizeIdentifier(input);
assert.strictEqual(result, "foo bar");
});

it("should preserve an empty string", () => {
const input = "";
const result = normalizeIdentifier(input);
assert.strictEqual(result, "");
});

it("should return an empty string for Markdown whitespace only", () => {
const input = " \t\r\n ";
const result = normalizeIdentifier(input);
assert.strictEqual(result, "");
});
});

describe("stripHtmlComments()", () => {
it("should replace single-line HTML comments with spaces", () => {
const input = "Hello<!--1234567-->World";
Expand Down