2024-01-09 23:20:17 +01:00
|
|
|
const { test } = require("node:test")
|
2023-10-23 23:11:41 +02:00
|
|
|
const assert = require("node:assert/strict")
|
2023-06-24 11:42:09 +02:00
|
|
|
|
|
|
|
const {
|
|
|
|
convertHeadingToHTMLFragment,
|
2023-10-23 23:11:41 +02:00
|
|
|
getMarkdownHeadings,
|
2024-01-12 00:43:45 +01:00
|
|
|
getMarkdownIdOrAnchorNameFragments,
|
2024-01-31 01:14:27 +01:00
|
|
|
isValidIntegerString,
|
|
|
|
getNumberOfLines,
|
|
|
|
getLineNumberStringFromFragment,
|
2023-10-23 23:11:41 +02:00
|
|
|
} = require("../src/utils.js")
|
2023-06-24 11:42:09 +02:00
|
|
|
|
2023-10-23 23:11:41 +02:00
|
|
|
test("utils", async (t) => {
|
|
|
|
await t.test("convertHeadingToHTMLFragment", async () => {
|
2023-06-24 11:42:09 +02:00
|
|
|
assert.strictEqual(
|
2023-10-23 23:11:41 +02:00
|
|
|
convertHeadingToHTMLFragment("Valid Fragments"),
|
|
|
|
"#valid-fragments",
|
2023-06-24 11:42:09 +02:00
|
|
|
)
|
|
|
|
assert.strictEqual(
|
2023-10-23 23:11:41 +02:00
|
|
|
convertHeadingToHTMLFragment("Valid Heading With Underscores _"),
|
|
|
|
"#valid-heading-with-underscores-_",
|
2023-06-24 11:42:09 +02:00
|
|
|
)
|
|
|
|
assert.strictEqual(
|
|
|
|
convertHeadingToHTMLFragment(
|
2023-10-23 23:11:41 +02:00
|
|
|
`Valid Heading With Quotes ' And Double Quotes "`,
|
2023-06-24 11:42:09 +02:00
|
|
|
),
|
2023-10-23 23:11:41 +02:00
|
|
|
"#valid-heading-with-quotes--and-double-quotes-",
|
2023-06-24 11:42:09 +02:00
|
|
|
)
|
|
|
|
assert.strictEqual(
|
2023-10-23 23:11:41 +02:00
|
|
|
convertHeadingToHTMLFragment("🚀 Valid Heading With Emoji"),
|
|
|
|
"#-valid-heading-with-emoji",
|
2023-06-24 11:42:09 +02:00
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2023-10-23 23:11:41 +02:00
|
|
|
await t.test("getMarkdownHeadings", async () => {
|
2023-06-24 11:42:09 +02:00
|
|
|
assert.deepStrictEqual(
|
2023-10-23 23:11:41 +02:00
|
|
|
getMarkdownHeadings("# Hello\n\n## World\n\n## Hello, world!\n"),
|
|
|
|
["Hello", "World", "Hello, world!"],
|
2023-06-24 11:42:09 +02:00
|
|
|
)
|
|
|
|
})
|
2024-01-11 14:42:34 +01:00
|
|
|
|
2024-01-12 00:43:45 +01:00
|
|
|
await t.test("getMarkdownIdOrAnchorNameFragments", async () => {
|
2024-01-11 14:42:34 +01:00
|
|
|
assert.deepStrictEqual(
|
2024-01-12 00:43:45 +01:00
|
|
|
getMarkdownIdOrAnchorNameFragments(
|
2024-01-12 00:33:05 +01:00
|
|
|
'<a name="anchorName" id="anchorId">Link</a>',
|
|
|
|
),
|
2024-01-11 14:42:34 +01:00
|
|
|
["#anchorId"],
|
|
|
|
)
|
|
|
|
assert.deepStrictEqual(
|
2024-01-12 00:43:45 +01:00
|
|
|
getMarkdownIdOrAnchorNameFragments('<a name="anchorName">Link</a>'),
|
2024-01-11 14:42:34 +01:00
|
|
|
["#anchorName"],
|
|
|
|
)
|
2024-01-12 00:43:45 +01:00
|
|
|
assert.deepStrictEqual(
|
|
|
|
getMarkdownIdOrAnchorNameFragments("<a>Link</a>"),
|
|
|
|
[],
|
|
|
|
)
|
|
|
|
assert.deepStrictEqual(getMarkdownIdOrAnchorNameFragments("<a>"), [])
|
|
|
|
assert.deepStrictEqual(getMarkdownIdOrAnchorNameFragments("<a id=>"), [])
|
2024-01-11 14:42:34 +01:00
|
|
|
})
|
2024-01-31 01:14:27 +01:00
|
|
|
|
|
|
|
await t.test("isValidIntegerString", async () => {
|
|
|
|
assert.strictEqual(isValidIntegerString("1"), true)
|
|
|
|
assert.strictEqual(isValidIntegerString("45"), true)
|
|
|
|
assert.strictEqual(isValidIntegerString("1abc"), false)
|
|
|
|
assert.strictEqual(isValidIntegerString("1.0"), false)
|
|
|
|
})
|
|
|
|
|
|
|
|
await t.test("getNumberOfLines", async () => {
|
|
|
|
assert.strictEqual(getNumberOfLines(""), 1)
|
|
|
|
assert.strictEqual(getNumberOfLines("Hello"), 1)
|
|
|
|
assert.strictEqual(getNumberOfLines("Hello\nWorld"), 2)
|
|
|
|
assert.strictEqual(getNumberOfLines("Hello\nWorld\n"), 3)
|
|
|
|
assert.strictEqual(getNumberOfLines("Hello\nWorld\n\n"), 4)
|
|
|
|
})
|
|
|
|
|
|
|
|
await t.test("getLineNumberStringFromFragment", async () => {
|
|
|
|
assert.strictEqual(getLineNumberStringFromFragment("#L50"), "50")
|
|
|
|
})
|
2023-06-24 11:42:09 +02:00
|
|
|
})
|