feat: usage of ESM modules imports (instead of CommonJS)

Fixes #10

BREAKING CHANGE: This package is now pure ESM

BREAKING CHANGE: minimum supported Node.js >= 22.0.0
This commit is contained in:
2024-12-28 22:52:51 +01:00
parent b4a04d2e8e
commit aa24db4fac
13 changed files with 1322 additions and 1143 deletions

5
src/index.d.ts vendored Normal file
View File

@ -0,0 +1,5 @@
import type { Rule } from "markdownlint"
declare const relativeLinksRule: Rule
export default relativeLinksRule

View File

@ -1,10 +1,8 @@
"use strict"
import { pathToFileURL } from "node:url"
import fs from "node:fs"
const { pathToFileURL } = require("node:url")
const fs = require("node:fs")
const { filterTokens } = require("./markdownlint-rule-helpers/helpers.js")
const {
import { filterTokens } from "./markdownlint-rule-helpers/helpers.js"
import {
convertHeadingToHTMLFragment,
getMarkdownHeadings,
getMarkdownIdOrAnchorNameFragments,
@ -12,14 +10,14 @@ const {
getNumberOfLines,
getLineNumberStringFromFragment,
lineFragmentRe,
} = require("./utils.js")
} from "./utils.js"
/** @typedef {import('markdownlint').Rule} MarkdownLintRule */
/**
* @type {MarkdownLintRule}
*/
const customRule = {
const relativeLinksRule = {
names: ["relative-links"],
description: "Relative links should be valid",
tags: ["links"],
@ -172,4 +170,4 @@ const customRule = {
},
}
module.exports = customRule
export default relativeLinksRule

View File

@ -14,7 +14,7 @@
* @param {(token: MarkdownItToken) => void} handler Callback function.
* @returns {void}
*/
const filterTokens = (params, type, handler) => {
export const filterTokens = (params, type, handler) => {
for (const token of params.parsers.markdownit.tokens) {
if (token.type === type) {
handler(token)
@ -28,11 +28,6 @@ const filterTokens = (params, type, handler) => {
* @param {string} name HTML attribute name.
* @returns {RegExp} Regular Expression for matching.
*/
const getHtmlAttributeRe = (name) => {
export const getHtmlAttributeRe = (name) => {
return new RegExp(`\\s${name}\\s*=\\s*['"]?([^'"\\s>]*)`, "iu")
}
module.exports = {
filterTokens,
getHtmlAttributeRe,
}

View File

@ -1,10 +1,10 @@
const MarkdownIt = require("markdown-it")
import MarkdownIt from "markdown-it"
const { getHtmlAttributeRe } = require("./markdownlint-rule-helpers/helpers.js")
import { getHtmlAttributeRe } from "./markdownlint-rule-helpers/helpers.js"
const markdownIt = new MarkdownIt({ html: true })
const lineFragmentRe = /^#(?:L\d+(?:C\d+)?-L\d+(?:C\d+)?|L\d+)$/
export const lineFragmentRe = /^#(?:L\d+(?:C\d+)?-L\d+(?:C\d+)?|L\d+)$/
/**
* Converts a Markdown heading into an HTML fragment according to the rules
@ -14,7 +14,7 @@ const lineFragmentRe = /^#(?:L\d+(?:C\d+)?-L\d+(?:C\d+)?|L\d+)$/
* @param {string} inlineText Inline token for heading.
* @returns {string} Fragment string for heading.
*/
const convertHeadingToHTMLFragment = (inlineText) => {
export const convertHeadingToHTMLFragment = (inlineText) => {
return (
"#" +
encodeURIComponent(
@ -40,7 +40,7 @@ const ignoredTokens = new Set(["heading_open", "heading_close"])
* @param {string} content
* @returns {string[]}
*/
const getMarkdownHeadings = (content) => {
export const getMarkdownHeadings = (content) => {
const tokens = markdownIt.parse(content, {})
/** @type {string[]} */
@ -88,7 +88,7 @@ const idHTMLAttributeRegex = getHtmlAttributeRe("id")
* @param {string} content
* @returns {string[]}
*/
const getMarkdownIdOrAnchorNameFragments = (content) => {
export const getMarkdownIdOrAnchorNameFragments = (content) => {
const tokens = markdownIt.parse(content, {})
/** @type {string[]} */
@ -128,7 +128,7 @@ const getMarkdownIdOrAnchorNameFragments = (content) => {
* @example isValidIntegerString("1abc") // false
* @example isValidIntegerString("1.0") // false
*/
const isValidIntegerString = (value) => {
export const isValidIntegerString = (value) => {
const regex = /^\d+$/
return regex.test(value)
}
@ -138,7 +138,7 @@ const isValidIntegerString = (value) => {
* @param {string} content
* @returns {number}
*/
const getNumberOfLines = (content) => {
export const getNumberOfLines = (content) => {
return content.split("\n").length
}
@ -148,16 +148,6 @@ const getNumberOfLines = (content) => {
* @returns {string}
* @example getLineNumberStringFromFragment("#L50") // 50
*/
const getLineNumberStringFromFragment = (fragment) => {
export const getLineNumberStringFromFragment = (fragment) => {
return fragment.slice(2)
}
module.exports = {
lineFragmentRe,
convertHeadingToHTMLFragment,
getMarkdownHeadings,
getMarkdownIdOrAnchorNameFragments,
isValidIntegerString,
getNumberOfLines,
getLineNumberStringFromFragment,
}