feat: add main function of render

This commit is contained in:
Yaotian-Liu 2023-03-13 19:40:20 +08:00
parent f3eb1ffb77
commit 0a3f22ed2b
No known key found for this signature in database
GPG key ID: 4AEEE42596AA7552
6 changed files with 2423 additions and 98 deletions

197
main.ts
View file

@ -1,78 +1,51 @@
import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian'; import { App, Notice, Plugin, PluginSettingTab, Setting, MarkdownView, Editor, Modal } from 'obsidian';
import * as pseudocode from "pseudocode";
// Remember to rename these classes and interfaces! // Remember to rename these classes and interfaces!
interface MyPluginSettings { interface PseudocodeSettings {
mySetting: string; indentSize: string,
commentDelimiter: string,
lineNumber: boolean,
lineNumberPunc: string,
noEnd: boolean,
captionCount: undefined
} }
const DEFAULT_SETTINGS: MyPluginSettings = { const DEFAULT_SETTINGS: PseudocodeSettings = {
mySetting: 'default' indentSize: '1.2em',
commentDelimiter: '//',
lineNumber: false,
lineNumberPunc: ':',
noEnd: false,
captionCount: undefined
} }
export default class MyPlugin extends Plugin { export default class PseudocodePlugin extends Plugin {
settings: MyPluginSettings; settings: PseudocodeSettings;
async pseudocodeHandler(source: string, el: HTMLElement, ctx: any): Promise<any> {
// const rawRows: string[] = source.split("\n");
const katex = el.createEl("script");
katex.src = "https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.11.1/katex.min.js";
katex.integrity = "sha256-F/Xda58SPdcUCr+xhSGz9MA2zQBPb0ASEYKohl8UCHc=";
katex.crossOrigin = "anonymous";
const preEl = el.createEl("pre", { cls: "code", text: source });
console.log(el);
pseudocode.renderElement(preEl, this.settings);
}
async onload() { async onload() {
await this.loadSettings(); await this.loadSettings();
// This creates an icon in the left ribbon. this.registerMarkdownCodeBlockProcessor("pcode", this.pseudocodeHandler.bind(this));
const ribbonIconEl = this.addRibbonIcon('dice', 'Sample Plugin', (evt: MouseEvent) => {
// Called when the user clicks the icon.
new Notice('This is a notice!');
});
// Perform additional things with the ribbon
ribbonIconEl.addClass('my-plugin-ribbon-class');
// This adds a status bar item to the bottom of the app. Does not work on mobile apps.
const statusBarItemEl = this.addStatusBarItem();
statusBarItemEl.setText('Status Bar Text');
// This adds a simple command that can be triggered anywhere
this.addCommand({
id: 'open-sample-modal-simple',
name: 'Open sample modal (simple)',
callback: () => {
new SampleModal(this.app).open();
}
});
// This adds an editor command that can perform some operation on the current editor instance
this.addCommand({
id: 'sample-editor-command',
name: 'Sample editor command',
editorCallback: (editor: Editor, view: MarkdownView) => {
console.log(editor.getSelection());
editor.replaceSelection('Sample Editor Command');
}
});
// This adds a complex command that can check whether the current state of the app allows execution of the command
this.addCommand({
id: 'open-sample-modal-complex',
name: 'Open sample modal (complex)',
checkCallback: (checking: boolean) => {
// Conditions to check
const markdownView = this.app.workspace.getActiveViewOfType(MarkdownView);
if (markdownView) {
// If checking is true, we're simply "checking" if the command can be run.
// If checking is false, then we want to actually perform the operation.
if (!checking) {
new SampleModal(this.app).open();
}
// This command will only show up in Command Palette when the check function returns true
return true;
}
}
});
// This adds a settings tab so the user can configure various aspects of the plugin // This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new SampleSettingTab(this.app, this)); this.addSettingTab(new PseudocodeSettingTab(this.app, this));
// If the plugin hooks up any global DOM events (on parts of the app that doesn't belong to this plugin)
// Using this function will automatically remove the event listener when this plugin is disabled.
this.registerDomEvent(document, 'click', (evt: MouseEvent) => {
console.log('click', evt);
});
// When registering intervals, this function will automatically clear the interval when the plugin is disabled. // When registering intervals, this function will automatically clear the interval when the plugin is disabled.
this.registerInterval(window.setInterval(() => console.log('setInterval'), 5 * 60 * 1000)); this.registerInterval(window.setInterval(() => console.log('setInterval'), 5 * 60 * 1000));
@ -91,26 +64,10 @@ export default class MyPlugin extends Plugin {
} }
} }
class SampleModal extends Modal { class PseudocodeSettingTab extends PluginSettingTab {
constructor(app: App) { plugin: PseudocodePlugin;
super(app);
}
onOpen() { constructor(app: App, plugin: PseudocodePlugin) {
const {contentEl} = this;
contentEl.setText('Woah!');
}
onClose() {
const {contentEl} = this;
contentEl.empty();
}
}
class SampleSettingTab extends PluginSettingTab {
plugin: MyPlugin;
constructor(app: App, plugin: MyPlugin) {
super(app, plugin); super(app, plugin);
this.plugin = plugin; this.plugin = plugin;
} }
@ -120,18 +77,80 @@ class SampleSettingTab extends PluginSettingTab {
containerEl.empty(); containerEl.empty();
containerEl.createEl('h2', {text: 'Settings for my awesome plugin.'}); containerEl.createEl('h1', { text: 'Pseudocode Plugin Settings' });
// Instantiate Indent Size setting
new Setting(containerEl) new Setting(containerEl)
.setName('Setting #1') .setName("Indent Size")
.setDesc('It\'s a secret') .setDesc("The indent size of inside a control block, e.g. if, for, etc. The unit must be in 'em'.")
.addText(text => text .addText(text => text
.setPlaceholder('Enter your secret') .setValue(this.plugin.settings.indentSize)
.setValue(this.plugin.settings.mySetting)
.onChange(async (value) => { .onChange(async (value) => {
console.log('Secret: ' + value); this.plugin.settings.indentSize = value;
this.plugin.settings.mySetting = value;
await this.plugin.saveSettings(); await this.plugin.saveSettings();
})); })
);
// Instantiate Comment Delimiter setting
new Setting(containerEl)
.setName("Comment Delimiter")
.setDesc("The string used to indicate a comment in the pseudocode.")
.addText(text => text
.setValue(this.plugin.settings.commentDelimiter)
.onChange(async (value) => {
this.plugin.settings.commentDelimiter = value;
await this.plugin.saveSettings();
})
);
// Instantiate Show Line Numbers setting
new Setting(containerEl)
.setName("Show Line Numbers")
.setDesc("Whether line numbering is enabled.")
.addToggle(toggle => toggle
.setValue(this.plugin.settings.lineNumber)
.onChange(async (value) => {
this.plugin.settings.lineNumber = value;
await this.plugin.saveSettings();
})
);
// Instantiate Line Number Punctuation setting
new Setting(containerEl)
.setName("Line Number Punctuation")
.setDesc("The punctuation used to separate the line number from the pseudocode.")
.addText(text => text
.setValue(this.plugin.settings.lineNumberPunc)
.onChange(async (value) => {
this.plugin.settings.lineNumberPunc = value;
await this.plugin.saveSettings();
})
);
// Instantiate No End setting
new Setting(containerEl)
.setName("No End")
.setDesc("If enabled, pseudocode blocks will not have an 'end' statement.")
.addToggle(toggle => toggle
.setValue(this.plugin.settings.noEnd)
.onChange(async (value) => {
this.plugin.settings.noEnd = value;
await this.plugin.saveSettings();
})
);
// Instantiate Caption Count setting
// new Setting(containerEl)
// .setName("Caption Count")
// .setDesc("The number to start captioning pseudocode blocks from.")
// .addText(text => text
// .setValue(this.plugin.settings.captionCount)
// .onChange(async (value) => {
// this.plugin.settings.captionCount = value;
// await this.plugin.saveSettings();
// })
// );
} }
} }
``

View file

@ -1,11 +1,11 @@
{ {
"id": "obsidian-sample-plugin", "id": "obsidian-pseudocode",
"name": "Sample Plugin", "name": "Pseudocode",
"version": "1.0.0", "version": "1.0.0",
"minAppVersion": "0.15.0", "minAppVersion": "0.15.0",
"description": "This is a sample plugin for Obsidian. This plugin demonstrates some of the capabilities of the Obsidian API.", "description": "This is an obsidian plugin that helps to render a LaTeX-style pseudocode inside a code block.",
"author": "Obsidian", "author": "Yaotian Liu",
"authorUrl": "https://obsidian.md", "authorUrl": "https://github.com/Yaotian-Liu",
"fundingUrl": "https://obsidian.md/pricing", "fundingUrl": "",
"isDesktopOnly": false "isDesktopOnly": false
} }

2219
package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

View file

@ -20,5 +20,8 @@
"obsidian": "latest", "obsidian": "latest",
"tslib": "2.4.0", "tslib": "2.4.0",
"typescript": "4.7.4" "typescript": "4.7.4"
},
"dependencies": {
"pseudocode": "latest"
} }
} }

6
pseudocode.d.ts vendored Normal file
View file

@ -0,0 +1,6 @@
declare module 'pseudocode' {
export class ParseError extends Error { }
export function render(input: string, baseDomEle?: Element, options?: any): Element;
export function renderToString(input: string, options?: any): string;
export function renderElement(elem: Element, options?: any): void;
}

View file

@ -6,3 +6,81 @@ available in the app when your plugin is enabled.
If your plugin does not need CSS, delete this file. If your plugin does not need CSS, delete this file.
*/ */
@import url(https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.11.1/katex.min.css);
.ps-root {
font-family: KaTeX_Main, 'Times New Roman', Times, serif;
font-size: 1em;
font-weight: 100;
-webkit-font-smoothing: antialiased !important
}
.ps-root .ps-algorithm {
margin: .8em 0;
border-top: 3px solid #000;
border-bottom: 2px solid #000
}
.ps-root .ps-algorithm.with-caption>.ps-line:first-child {
border-bottom: 2px solid #000
}
.ps-root .katex {
text-indent: 0;
font-size: 1em
}
.ps-root .MathJax,
.ps-root .MathJax_CHTML {
text-indent: 0;
font-size: 1em !important
}
.ps-root .ps-line {
margin: 0;
padding: 0;
line-height: 1.2
}
.ps-root .ps-funcname {
font-family: KaTeX_Main, 'Times New Roman', Times, serif;
font-weight: 400;
font-variant: small-caps;
font-style: normal;
text-transform: none
}
.ps-root .ps-keyword {
font-family: KaTeX_Main, 'Times New Roman', Times, serif;
font-weight: 700;
font-variant: normal;
font-style: normal;
text-transform: none
}
.ps-root .ps-comment {
font-family: KaTeX_Main, 'Times New Roman', Times, serif;
font-weight: 400;
font-variant: normal;
font-style: normal;
text-transform: none
}
.ps-root .ps-linenum {
font-size: .8em;
line-height: 1em;
width: 1.6em;
text-align: right;
display: inline-block;
position: relative;
padding-right: .3em
}
.ps-root .ps-algorithmic.with-linenum .ps-line.ps-code {
text-indent: -1.6em
}
.ps-root .ps-algorithmic.with-linenum .ps-line.ps-code>span {
text-indent: 0
}