Skip to content

Commit

Permalink
feat: display toc as inline block
Browse files Browse the repository at this point in the history
  • Loading branch information
fnumatic committed Feb 7, 2024
1 parent bcf9952 commit 678401f
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ export const DEFAULT_SETTINGS: DynamicTOCSettings = {
externalStyle: "None",
supportAllMatchers: false,
allow_inconsistent_headings: false,
embeddedHeadings : true
embeddedHeadings: true,
};

export const TABLE_CLASS_NAME = "dynamic-toc";
export const TABLE_CLASS_NAME_INLINE = "dynamic-toc-inline";
export const TABLE_CLASS_SELECTOR = `.${TABLE_CLASS_NAME}`;

export const ALL_MATCHERS = Object.keys(
Expand Down
13 changes: 12 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Editor, MarkdownPostProcessorContext, Plugin } from "obsidian";
import { parseConfig } from "./utils/config";
import { mergeSettings, parseConfig } from "./utils/config";
import { ALL_MATCHERS, DEFAULT_SETTINGS } from "./constants";
import { CodeBlockRenderer } from "./renderers/code-block-renderer";
import { DynamicTOCSettingsTab } from "./settings-tab";
import {
DynamicTOCSettings,
ExternalMarkdownKey,
EXTERNAL_MARKDOWN_PREVIEW_STYLE,
TableOptions,
INLINE_TOC_MATCHER,
} from "./types";
import { DynamicInjectionRenderer } from "./renderers/dynamic-injection-renderer";
import { InsertCommandModal } from "./insert-command.modal";
Expand Down Expand Up @@ -40,6 +42,15 @@ export default class DynamicTOCPlugin extends Plugin {

this.registerMarkdownPostProcessor(
(el: HTMLElement, ctx: MarkdownPostProcessorContext) => {
const codeblocks = el.findAll("code");
const options = mergeSettings({ style: "inline" , displayInline: true } as TableOptions, this.settings);

for (let codeblock of codeblocks) {
if (codeblock.innerText.trim() != INLINE_TOC_MATCHER) continue
ctx.addChild(
new CodeBlockRenderer(this.app, options, ctx.sourcePath, codeblock)
);
}
const matchers =
this.settings.supportAllMatchers === true
? ALL_MATCHERS
Expand Down
7 changes: 5 additions & 2 deletions src/renderers/code-block-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import { mergeSettings } from "../utils/config";
import { extractHeadings, embeddedHeadings, mergeHeadings } from "../utils/extract-headings";
import { DynamicTOCSettings, TableOptions } from "../types";
import { TABLE_CLASS_NAME } from "src/constants";
import { TABLE_CLASS_NAME, TABLE_CLASS_NAME_INLINE } from "src/constants";

export class CodeBlockRenderer extends MarkdownRenderChild {
constructor(
Expand Down Expand Up @@ -56,8 +56,11 @@ export class CodeBlockRenderer extends MarkdownRenderChild {

async render(configOverride?: TableOptions) {
const settings= configOverride || this.config
const cls= settings.displayInline ? TABLE_CLASS_NAME_INLINE : null
this.container.empty();
this.container.classList.add(TABLE_CLASS_NAME);
this.container = this.container.createSpan();
this.container.classList.add(TABLE_CLASS_NAME,cls);

const fileMetaData = this.app.metadataCache.getCache(this.filePath)
const { headings, embeds } = fileMetaData;
const embbedHeadings = embeddedHeadings(this.app.metadataCache, embeds)
Expand Down
4 changes: 4 additions & 0 deletions src/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@
.is-live-preview .dynamic-toc ol {
white-space: normal;
}

.dynamic-toc-inline > p{
display: inline;
}
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface TableOptions {
delimiter?: string;
varied_style?: boolean;
embeddedHeadings: boolean;
displayInline?: boolean;
}

export const EXTERNAL_MARKDOWN_PREVIEW_STYLE = {
Expand All @@ -19,6 +20,8 @@ export const EXTERNAL_MARKDOWN_PREVIEW_STYLE = {
TheBrain: "[/toc/]",
};

export const INLINE_TOC_MATCHER = "toc@@inline";

export type ExternalMarkdownKey = keyof typeof EXTERNAL_MARKDOWN_PREVIEW_STYLE;
export interface DynamicTOCSettings extends TableOptions {
externalStyle: ExternalMarkdownKey;
Expand Down

0 comments on commit 678401f

Please sign in to comment.