Skip to content

Commit

Permalink
Basic tests for options & headings
Browse files Browse the repository at this point in the history
  • Loading branch information
johansatge committed Oct 30, 2023
1 parent 015bb21 commit 0543865
Show file tree
Hide file tree
Showing 7 changed files with 3,711 additions and 11 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: test
on: push
jobs:
build:
runs-on: ubuntu-latest
timeout-minutes: 1
strategy:
matrix:
node-version: [18.x]
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- name: Cache node_modules
id: cache-node-modules
uses: actions/cache@v3
with:
key: ${{ runner.os }}-${{ matrix.node-version }}-${{ hashFiles('**/package-lock.json') }}
path: node_modules
- name: Install dependencies
if: steps.cache-node-modules.outputs.cache-hit != 'true'
run: npm ci
- name: Run tests
run: npm test
44 changes: 33 additions & 11 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
const { Plugin, MarkdownRenderer, MarkdownRenderChild } = require('obsidian')
let Plugin = class {}
let MarkdownRenderer = {}
let MarkdownRenderChild = class {}

const isObsidian = !process.env.JEST_WORKER_ID

if (isObsidian) {
const osbidian = require('obsidian')
Plugin = osbidian.Plugin
MarkdownRenderer = osbidian.MarkdownRenderer
MarkdownRenderChild = osbidian.MarkdownRenderChild
}

const codeblockId = 'table-of-contents'
const availableOptions = {
Expand All @@ -25,7 +36,7 @@ const availableOptions = {
},
}

module.exports = class extends Plugin {
class ObsidianAutomaticTableOfContents extends Plugin {
async onload() {
this.registerMarkdownCodeBlockProcessor(codeblockId, (sourceText, element, context) => {
context.addChild(new Renderer(this.app, element, context.sourcePath, sourceText))
Expand Down Expand Up @@ -87,14 +98,7 @@ class Renderer extends MarkdownRenderChild {
const headings = metadata && metadata.headings ? metadata.headings : []
if (options.debugInConsole) debug('Headings', headings)

const markdownHandlersByStyle = {
nestedList: getMarkdownNestedListFromHeadings,
inlineFirstLevel: getMarkdownInlineFirstLevelFromHeadings,
}
let markdown = markdownHandlersByStyle[options.style](headings, options)
if (markdown === null) {
markdown = '_Table of contents: no headings found_'
}
const markdown = getMarkdownFromHeadings(headings, options)
if (options.debugInConsole) debug('Markdown', markdown)

this.element.empty()
Expand All @@ -106,6 +110,15 @@ class Renderer extends MarkdownRenderChild {
}
}

function getMarkdownFromHeadings(headings, options) {
const markdownHandlersByStyle = {
nestedList: getMarkdownNestedListFromHeadings,
inlineFirstLevel: getMarkdownInlineFirstLevelFromHeadings,
}
let markdown = markdownHandlersByStyle[options.style](headings, options)
return markdown || '_Table of contents: no headings found_'
}

function getMarkdownNestedListFromHeadings(headings, options) {
const lines = []
headings.forEach((heading) => {
Expand Down Expand Up @@ -166,4 +179,13 @@ function parseOptionFromSourceLine(line) {

function debug() {
console.log(`%cAutomatic Table Of Contents`, 'color: orange; font-weight: bold', ...arguments)
}
}

if (isObsidian) {
module.exports = ObsidianAutomaticTableOfContents
} else {
module.exports = {
parseOptionsFromSourceText,
getMarkdownFromHeadings,
}
}
Loading

0 comments on commit 0543865

Please sign in to comment.