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 31, 2023
1 parent 015bb21 commit 5f2707a
Show file tree
Hide file tree
Showing 8 changed files with 3,843 additions and 12 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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Obsidian Automatic Table Of Contents

[![Version](https://img.shields.io/github/v/release/johansatge/obsidian-automatic-table-of-contents)](https://github.com/johansatge/obsidian-automatic-table-of-contents/releases)
[![Test](https://github.com/johansatge/httpdir/actions/workflows/test.yml/badge.svg)](https://github.com/johansatge/httpdir/actions)

> An Obsidian plugin to create a table of contents in a note, that updates itself when the note changes
---
Expand Down
46 changes: 34 additions & 12 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 All @@ -127,7 +140,7 @@ function getMarkdownInlineFirstLevelFromHeadings(headings, options) {
return items.length > 0 ? items.join(' | ') : null
}

function parseOptionsFromSourceText(sourceText) {
function parseOptionsFromSourceText(sourceText = '') {
const options = {}
Object.keys(availableOptions).forEach((option) => {
options[option] = availableOptions[option].default
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 5f2707a

Please sign in to comment.