vault backup: 2025-04-11 14:41:26

This commit is contained in:
SeedList
2025-04-11 14:41:27 +08:00
parent eb4274e3e8
commit e1a3e955af
19 changed files with 4484 additions and 1 deletions

View File

@ -0,0 +1,218 @@
const {
getMarkdownFromHeadings,
parseOptionsFromSourceText,
} = require('../main.js')
const testStandardHeadings = [
{ heading: 'Title 1 level 1', level: 1 },
{ heading: 'Title 1 level 2', level: 2 },
{ heading: 'Title 1 level 3', level: 3 },
{ heading: 'Title 2 level 1', level: 1 },
{ heading: 'Title 3 level 1', level: 1 },
{ heading: 'Title 3 level 2', level: 2 },
]
const testHeadingsWithoutFirstLevel = [
{ heading: 'Title 1 level 2', level: 2 },
{ heading: 'Title 1 level 3', level: 3 },
{ heading: 'Title 1 level 4', level: 4 },
{ heading: 'Title 2 level 2', level: 2 },
{ heading: 'Title 3 level 2', level: 2 },
{ heading: 'Title 3 level 3', level: 3 },
]
const testHeadingsWithSpecialChars = [
{ heading: 'Title 1 `level 1` {with special chars}, **bold**, _italic_, #a-tag, ==highlighted== and ~~strikethrough~~ text', level: 1 },
{ heading: 'Title 1 level 2 <em style="color: black">with HTML</em>', level: 2 },
{ heading: 'Title 1 level 2 [[wikilink1]] [[wikilink2|wikitext2]] [mdlink](https://mdurl)', level: 2 },
{ heading: 'Title 1 level 2 [[malformedlink a pi|pe | and [other chars]', level: 2 },
]
describe('Headings', () => {
test('Returns default message if no headings', () => {
const options = parseOptionsFromSourceText('')
const md = getMarkdownFromHeadings([], options)
expect(md).toContain('no headings found')
})
test('Returns empty TOC if no headings & option enabled', () => {
const options = parseOptionsFromSourceText('')
options.hideWhenEmpty = true
const md = getMarkdownFromHeadings([], options)
expect(md).toEqual('')
})
test('Returns indented list with links', () => {
const options = parseOptionsFromSourceText('')
const md = getMarkdownFromHeadings(testStandardHeadings, options)
const expectedMd = sanitizeMd(`
- [[#Title 1 level 1|Title 1 level 1]]
- [[#Title 1 level 2|Title 1 level 2]]
- [[#Title 1 level 3|Title 1 level 3]]
- [[#Title 2 level 1|Title 2 level 1]]
- [[#Title 3 level 1|Title 3 level 1]]
- [[#Title 3 level 2|Title 3 level 2]]
`)
expect(md).toEqual(expectedMd)
})
test('Returns indented list with links if no first level', () => {
const options = parseOptionsFromSourceText('')
const md = getMarkdownFromHeadings(testHeadingsWithoutFirstLevel, options)
const expectedMd = sanitizeMd(`
- [[#Title 1 level 2|Title 1 level 2]]
- [[#Title 1 level 3|Title 1 level 3]]
- [[#Title 1 level 4|Title 1 level 4]]
- [[#Title 2 level 2|Title 2 level 2]]
- [[#Title 3 level 2|Title 3 level 2]]
- [[#Title 3 level 3|Title 3 level 3]]
`)
expect(md).toEqual(expectedMd)
})
test('Returns indented ordered list with links', () => {
const options = parseOptionsFromSourceText('')
options.style = 'nestedOrderedList'
const md = getMarkdownFromHeadings(testStandardHeadings, options)
const expectedMd = sanitizeMd(`
1. [[#Title 1 level 1|Title 1 level 1]]
1. [[#Title 1 level 2|Title 1 level 2]]
1. [[#Title 1 level 3|Title 1 level 3]]
1. [[#Title 2 level 1|Title 2 level 1]]
1. [[#Title 3 level 1|Title 3 level 1]]
1. [[#Title 3 level 2|Title 3 level 2]]
`)
expect(md).toEqual(expectedMd)
})
test('Returns indented list with min level', () => {
const options = parseOptionsFromSourceText('')
options.minLevel = 2
const md = getMarkdownFromHeadings(testStandardHeadings, options)
const expectedMd = sanitizeMd(`
- [[#Title 1 level 2|Title 1 level 2]]
- [[#Title 1 level 3|Title 1 level 3]]
- [[#Title 3 level 2|Title 3 level 2]]
`)
expect(md).toEqual(expectedMd)
})
test('Returns indented list with max level', () => {
const options = parseOptionsFromSourceText('')
options.maxLevel = 2
const md = getMarkdownFromHeadings(testStandardHeadings, options)
const expectedMd = sanitizeMd(`
- [[#Title 1 level 1|Title 1 level 1]]
- [[#Title 1 level 2|Title 1 level 2]]
- [[#Title 2 level 1|Title 2 level 1]]
- [[#Title 3 level 1|Title 3 level 1]]
- [[#Title 3 level 2|Title 3 level 2]]
`)
expect(md).toEqual(expectedMd)
})
test('Returns indented list without links', () => {
const options = parseOptionsFromSourceText('')
options.includeLinks = false
const md = getMarkdownFromHeadings(testStandardHeadings, options)
const expectedMd = sanitizeMd(`
- Title 1 level 1
- Title 1 level 2
- Title 1 level 3
- Title 2 level 1
- Title 3 level 1
- Title 3 level 2
`)
expect(md).toEqual(expectedMd)
})
test('Returns indented list with title', () => {
const options = parseOptionsFromSourceText('')
options.title = '# My TOC'
const md = getMarkdownFromHeadings(testStandardHeadings, options)
const expectedMd = sanitizeMd(`
# My TOC
- [[#Title 1 level 1|Title 1 level 1]]
- [[#Title 1 level 2|Title 1 level 2]]
- [[#Title 1 level 3|Title 1 level 3]]
- [[#Title 2 level 1|Title 2 level 1]]
- [[#Title 3 level 1|Title 3 level 1]]
- [[#Title 3 level 2|Title 3 level 2]]
`)
expect(md).toEqual(expectedMd)
})
test('Returns indented list with sanitized links from special chars', () => {
const options = parseOptionsFromSourceText('')
const md = getMarkdownFromHeadings(testHeadingsWithSpecialChars, options)
const expectedMd = sanitizeMd(`
- [[#Title 1 \`level 1\` {with special chars}, **bold**, _italic_, a-tag, ==highlighted== and ~~strikethrough~~ text|Title 1 level 1 {with special chars}, bold, italic, #a-tag, highlighted and strikethrough text]]
- [[#Title 1 level 2 <em style="color: black">with HTML</em>|Title 1 level 2 <em style="color: black">with HTML</em>]]
- [[#Title 1 level 2 wikilink1 wikilink2 wikitext2 [mdlink](https://mdurl)|Title 1 level 2 wikilink1 wikitext2 mdlink]]
- [[#Title 1 level 2 malformedlink a pi pe and [other chars]|Title 1 level 2 malformedlink a pi-pe - and [other chars]]]
`)
expect(md).toEqual(expectedMd)
})
test('Returns indented list without links from special chars', () => {
const options = parseOptionsFromSourceText('')
options.includeLinks = false
const md = getMarkdownFromHeadings(testHeadingsWithSpecialChars, options)
const expectedMd = sanitizeMd(`
- Title 1 \`level 1\` {with special chars}, **bold**, _italic_, #a-tag, ==highlighted== and ~~strikethrough~~ text
- Title 1 level 2 <em style="color: black">with HTML</em>
- Title 1 level 2 [[wikilink1]] [[wikilink2|wikitext2]] [mdlink](https://mdurl)
- Title 1 level 2 [[malformedlink a pi|pe | and [other chars]
`)
expect(md).toEqual(expectedMd)
})
test('Returns flat first-level list with links', () => {
const options = parseOptionsFromSourceText('')
options.style = 'inlineFirstLevel'
const md = getMarkdownFromHeadings(testStandardHeadings, options)
const expectedMd = sanitizeMd(`
[[#Title 1 level 1|Title 1 level 1]] | [[#Title 2 level 1|Title 2 level 1]] | [[#Title 3 level 1|Title 3 level 1]]
`)
expect(md).toEqual(expectedMd)
})
test('Returns flat first-level list without links', () => {
const options = parseOptionsFromSourceText('')
options.style = 'inlineFirstLevel'
options.includeLinks = false
const md = getMarkdownFromHeadings(testStandardHeadings, options)
const expectedMd = sanitizeMd(`
Title 1 level 1 | Title 2 level 1 | Title 3 level 1
`)
expect(md).toEqual(expectedMd)
})
test('Returns flat list with custom level', () => {
const options = parseOptionsFromSourceText('')
options.style = 'inlineFirstLevel'
options.includeLinks = false
options.minLevel = 3
const md = getMarkdownFromHeadings(testStandardHeadings, options)
const expectedMd = sanitizeMd(`
Title 1 level 3
`)
expect(md).toEqual(expectedMd)
})
test('Returns flat list with default level', () => {
const options = parseOptionsFromSourceText('')
options.style = 'inlineFirstLevel'
options.includeLinks = false
const md = getMarkdownFromHeadings(testHeadingsWithoutFirstLevel, options)
const expectedMd = sanitizeMd(`
Title 1 level 2 | Title 2 level 2 | Title 3 level 2
`)
expect(md).toEqual(expectedMd)
})
})
function sanitizeMd(md) {
return md.replaceAll(' ', '\t').replace(/^\n/, '').replace(/\n$/, '')
}

View File

@ -0,0 +1,8 @@
const path = require('path')
module.exports = async () => {
return {
testMatch: [path.join(__dirname, '**/*.test.js')],
testTimeout: 1000,
}
}

View File

@ -0,0 +1,102 @@
const { parseOptionsFromSourceText } = require('../main.js')
describe('Options', () => {
test('Returns default options if none are specified', () => {
const options = parseOptionsFromSourceText('')
expect(options).toEqual({
title: '',
style: 'nestedList',
includeLinks: true,
minLevel: 0,
maxLevel: 0,
hideWhenEmpty: false,
debugInConsole: false,
})
})
test('Returns custom options if specified', () => {
const optionsText = `
title: # Some title
style: inlineFirstLevel # Some comment
minLevel: 1
maxLevel: 2 # Some other comment
includeLinks: false
hideWhenEmpty: true
debugInConsole: true
`
const options = parseOptionsFromSourceText(optionsText)
expect(options).toEqual({
title: '# Some title',
style: 'inlineFirstLevel',
includeLinks: false,
minLevel: 1,
maxLevel: 2,
hideWhenEmpty: true,
debugInConsole: true,
})
})
test('Accepts comments in options', () => {
const options = parseOptionsFromSourceText('maxLevel: 5 # some comment')
expect(options.maxLevel).toEqual(5)
})
test('Ignores unknown options', () => {
const options = parseOptionsFromSourceText(`
maxLevel: 5
someOption: someValue
`)
expect(options.maxLevel).toEqual(5)
})
describe('Throw an error if the option value is invalid', () => {
test('On style', () => {
try {
const options = parseOptionsFromSourceText('style: someInvalidStyle')
expect(options.style).toEqual('Should have thrown')
} catch(error) {
expect(error.message).toContain('Invalid value')
}
})
test('On minLevel', () => {
try {
const options = parseOptionsFromSourceText('minLevel: -1')
expect(options.minLevel).toEqual('Should have thrown')
} catch(error) {
expect(error.message).toContain('Invalid value')
}
})
test('On maxLevel', () => {
try {
const options = parseOptionsFromSourceText('maxLevel: -1')
expect(options.maxLevel).toEqual('Should have thrown')
} catch(error) {
expect(error.message).toContain('Invalid value')
}
})
test('On includeLinks', () => {
try {
const options = parseOptionsFromSourceText('includeLinks: no')
expect(options.includeLinks).toEqual('Should have thrown')
} catch(error) {
expect(error.message).toContain('Invalid value')
}
})
test('On hideWhenEmpty', () => {
try {
const options = parseOptionsFromSourceText('hideWhenEmpty: maybe')
expect(options.hideWhenEmpty).toEqual('Should have thrown')
} catch(error) {
expect(error.message).toContain('Invalid value')
}
})
test('On debugInConsole', () => {
try {
const options = parseOptionsFromSourceText('debugInConsole: yes')
expect(options.debugInConsole).toEqual('Should have thrown')
} catch(error) {
expect(error.message).toContain('Invalid value')
}
})
})
})