From f406dd8777d5daba29ee4fc6f8d8bb5c1427bf73 Mon Sep 17 00:00:00 2001 From: Seth Miers Date: Wed, 26 Nov 2025 22:14:40 -0700 Subject: [PATCH] Added support for changing the codeblock Language name, this is helpful when integrating with sphinx --- .prettierrc | 7 ++ manifest.json | 4 +- package.json | 1 + src/main.ts | 124 ++++++++++++++++----------- src/processors.ts | 213 +++++++++++++++++++++++++--------------------- src/setting.ts | 119 ++++++++++++++++---------- src/suggesters.ts | 142 ++++++++++++++----------------- 7 files changed, 336 insertions(+), 274 deletions(-) create mode 100644 .prettierrc diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..76f73ba --- /dev/null +++ b/.prettierrc @@ -0,0 +1,7 @@ +{ + "semi": true, + "singleQuote": true, + "trailingComma": "es5", + "printWidth": 100, + "tabWidth": 4 +} diff --git a/manifest.json b/manifest.json index 402ab79..6337d0d 100644 --- a/manifest.json +++ b/manifest.json @@ -1,10 +1,10 @@ { "id": "obsidian-graphviz", "name": "Obsidian Graphviz", - "version": "1.0.5", + "version": "1.0.6", "minAppVersion": "0.11.5", "description": "Render Graphviz Diagrams", - "author": "Feng Peng", + "author": "Feng Peng, Seth Miers", "authorUrl": "https://QAMichaelPeng.github.io", "isDesktopOnly": true } diff --git a/package.json b/package.json index c646903..4a7b010 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "eslint": "7.20.0", "eslint-plugin-json": "2.1.2", "obsidian": "^0.15.9", + "prettier": "^3.6.2", "tmp": "0.2.1", "tslib": "2.3.1", "typescript": "4.4.4" diff --git a/src/main.ts b/src/main.ts index be3ec8e..62ef4dd 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,56 +1,82 @@ -import { Plugin } from 'obsidian'; +import { Plugin, MarkdownPostProcessorContext } from 'obsidian'; import { DEFAULT_SETTINGS, GraphvizSettings, GraphvizSettingsTab } from './setting'; import { Processors } from './processors'; import { Suggesters } from './suggesters'; -// Remember to rename these classes and interfaces! +export default class GraphvizPlugin extends Plugin { + settings: GraphvizSettings; + registeredProcessors: (() => void)[] = []; -export default class GraphvizPlugin extends Plugin { - settings: GraphvizSettings; - - async onload() { - console.debug('Load graphviz plugin'); - await this.loadSettings(); - this.addSettingTab(new GraphvizSettingsTab(this)); - const processors = new Processors(this); - const suggesters = new Suggesters(this); - const d3Sources = ['https://d3js.org/d3.v5.min.js', - 'https://unpkg.com/@hpcc-js/wasm@0.3.11/dist/index.min.js', - 'https://unpkg.com/d3-graphviz@3.0.5/build/d3-graphviz.js']; - - - this.app.workspace.onLayoutReady(() => { - switch (this.settings.renderer) { - case 'd3_graphviz': - for (const src of d3Sources) { - const script = document.createElement('script'); - script.src = src; - (document.head || document.documentElement).appendChild(script); - } - this.registerMarkdownCodeBlockProcessor('dot', processors.d3graphvizProcessor.bind(processors)); - break; - default: - this.registerMarkdownCodeBlockProcessor('dot', processors.imageProcessor.bind(processors)); - } - - this.registerEditorSuggest(new Suggesters(this.app, this)); - }); - } - - - - onunload() { - console.debug('Unload graphviz plugin'); - } - - async loadSettings(): Promise { - this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData()); - return Promise.resolve(); - } - - - async saveSettings() { - await this.saveData(this.settings); - } + async onload() { + console.debug('Load graphviz plugin'); + await this.loadSettings(); + this.addSettingTab(new GraphvizSettingsTab(this)); + + const d3Sources = [ + 'https://d3js.org/d3.v5.min.js', + 'https://unpkg.com/@hpcc-js/wasm@0.3.11/dist/index.min.js', + 'https://unpkg.com/d3-graphviz@3.0.5/build/d3-graphviz.js', + ]; + + this.app.workspace.onLayoutReady(() => { + for (const src of d3Sources) { + const script = document.createElement('script'); + script.src = src; + (document.head || document.documentElement).appendChild(script); + } + + this.reloadProcessors(); + + this.registerEditorSuggest(new Suggesters(this.app, this)); + }); + } + + reloadProcessors() { + // unregister old processors + this.registeredProcessors.forEach((unreg) => unreg()); + this.registeredProcessors = []; + + const processors = new Processors(this); + const targetLang = this.settings.codeblockLanguage; + + // Wrap the processor to check both lang and label + const wrappedProcessor = async (source: string, el: HTMLElement, ctx: MarkdownPostProcessorContext) => { + const section = ctx.getSectionInfo(el); + if (!section) return; + + const firstLine = section.text.split('\n')[section.lineStart]; + const match = firstLine.match(/^```(?[^\s^{]+)?\s*(?:{(?