Skip to content

Commit

Permalink
Merge pull request #25 from ras0q/feat/minlevel
Browse files Browse the repository at this point in the history
add minLevel option
  • Loading branch information
johansatge committed Jan 3, 2024
2 parents cb39afc + 97609e6 commit f5650f5
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ The following options are available:
| Option | Default value | Description |
| --- | --- | --- |
| `style` | `nestedList` | Table of contents style (can be `nestedList` or `inlineFirstLevel`) |
| `minLevel` | `0` | Include headings from the speficied level |
| `maxLevel` | `0` | Include headings up to the speficied level (`0` for no limit) |
| `includeLinks` | `true` | Make headings clickable |
| `debugInConsole` | `false` | Print debug info in Obsidian console |
Expand Down
12 changes: 10 additions & 2 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ const availableOptions = {
values: ['nestedList', 'inlineFirstLevel'],
comment: 'TOC style (nestedList|inlineFirstLevel)',
},
minLevel: {
type: 'number',
default: 0,
comment: 'Include headings from the speficied level'
},
maxLevel: {
type: 'number',
default: 0,
Expand Down Expand Up @@ -98,7 +103,7 @@ class Renderer extends MarkdownRenderChild {

const markdown = getMarkdownFromHeadings(headings, options)
if (options.debugInConsole) debug('Markdown', markdown)

this.element.empty()
MarkdownRenderer.renderMarkdown(markdown, this.element, this.sourcePath, this)
} catch(error) {
Expand All @@ -119,8 +124,11 @@ function getMarkdownFromHeadings(headings, options) {

function getMarkdownNestedListFromHeadings(headings, options) {
const lines = []
const minLevel = Math.min(...headings.map((heading) => heading.level))
const minLevel = options.minLevel > 0
? options.minLevel
: Math.min(...headings.map((heading) => heading.level))
headings.forEach((heading) => {
if (heading.level < minLevel) return
if (options.maxLevel > 0 && heading.level > options.maxLevel) return
lines.push(`${'\t'.repeat(heading.level - minLevel)}- ${getMarkdownHeading(heading, options)}`)
})
Expand Down
14 changes: 13 additions & 1 deletion test/headings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ describe('Headings', () => {
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 3]]
- [[#Title 3 level 2]]
`)
expect(md).toEqual(expectedMd)
})

test('Returns indented list with max level', () => {
const options = parseOptionsFromSourceText('')
options.maxLevel = 2
Expand Down Expand Up @@ -103,4 +115,4 @@ Title [1] | level 1 | Title 2 level 1 | Title 3 level 1

function sanitizeMd(md) {
return md.replaceAll(' ', '\t').replace(/^\n/, '').replace(/\n$/, '')
}
}
11 changes: 11 additions & 0 deletions test/options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ describe('Options', () => {
expect(options).toEqual({
style: 'nestedList',
includeLinks: true,
minLevel: 0,
maxLevel: 0,
debugInConsole: false,
})
Expand All @@ -14,6 +15,7 @@ describe('Options', () => {
test('Returns custom options if specified', () => {
const optionsText = `
style: inlineFirstLevel
minLevel: 1
maxLevel: 2
includeLinks: false
debugInConsole: true
Expand All @@ -22,6 +24,7 @@ describe('Options', () => {
expect(options).toEqual({
style: 'inlineFirstLevel',
includeLinks: false,
minLevel: 1,
maxLevel: 2,
debugInConsole: true,
})
Expand Down Expand Up @@ -49,6 +52,14 @@ describe('Options', () => {
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')
Expand Down

0 comments on commit f5650f5

Please sign in to comment.