mirror of
https://github.com/ytliu74/obsidian-pseudocode.git
synced 2026-07-22 07:40:25 +00:00
Compare commits
No commits in common. "master" and "1.2.3" have entirely different histories.
16 changed files with 77 additions and 561 deletions
|
|
@ -18,7 +18,6 @@
|
|||
"@typescript-eslint/no-unused-vars": ["error", { "args": "none" }],
|
||||
"@typescript-eslint/ban-ts-comment": "off",
|
||||
"no-prototype-builtins": "off",
|
||||
"@typescript-eslint/no-empty-function": "off",
|
||||
"@typescript-eslint/no-inferrable-types": "off"
|
||||
"@typescript-eslint/no-empty-function": "off"
|
||||
}
|
||||
}
|
||||
4
.github/workflows/release.yml
vendored
4
.github/workflows/release.yml
vendored
|
|
@ -21,9 +21,7 @@ jobs:
|
|||
node-version: "18"
|
||||
|
||||
- name: Install Dependencies
|
||||
run: |
|
||||
npm config set registry https://registry.npmjs.org/
|
||||
npm install
|
||||
run: npm install
|
||||
|
||||
- name: Build
|
||||
run: npm run build
|
||||
|
|
|
|||
94
CLAUDE.md
94
CLAUDE.md
|
|
@ -1,94 +0,0 @@
|
|||
# CLAUDE.md
|
||||
|
||||
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
||||
|
||||
## Project Overview
|
||||
|
||||
Obsidian-Pseudocode is an Obsidian plugin that renders LaTeX-style pseudocode inside code blocks. It uses pseudocode.js to convert LaTeX algorithmic constructs to HTML, with support for math formulas via KaTeX.
|
||||
|
||||
## Build and Development Commands
|
||||
|
||||
```bash
|
||||
# Development mode with file watching
|
||||
npm run dev
|
||||
|
||||
# Production build (includes TypeScript type checking)
|
||||
npm run build
|
||||
|
||||
# Version bump
|
||||
npm run version
|
||||
```
|
||||
|
||||
## Architecture
|
||||
|
||||
### Plugin Entry Point
|
||||
- `main.ts`: Core plugin class (`PseudocodePlugin`)
|
||||
- Registers markdown code block processor for `pseudo` language
|
||||
- Manages settings and preamble loading
|
||||
- Handles theme observer lifecycle
|
||||
|
||||
### Core Processing Flow
|
||||
1. User creates a code block with `pseudo` language specifier
|
||||
2. `pseudocodeHandler()` in main.ts processes the block:
|
||||
- Extracts inline macros (before `\begin{algorithm}`)
|
||||
- Combines with global preamble if enabled
|
||||
- Injects preamble into all math expressions (`$...$`)
|
||||
- Renders using pseudocode.js library
|
||||
- Adds export button and applies theme
|
||||
|
||||
### Key Modules
|
||||
|
||||
**src/inline_macro.ts**
|
||||
- Separates inline macro definitions from algorithm content
|
||||
- Macros before `\begin{algorithm}` are treated as preamble
|
||||
|
||||
**src/latex_translator.ts**
|
||||
- Converts unsupported LaTeX macros to KaTeX-compatible commands
|
||||
- Handles `\DeclarePairedDelimiter`, `\DeclareMathOperator*`, `\DeclareMathOperator`
|
||||
- Validates macros using KaTeX parser and converts `\newcommand` to `\renewcommand` when redefining
|
||||
|
||||
**src/export_button.ts**
|
||||
- Creates "Export to clipboard" button for each pseudocode block
|
||||
- Generates compilable LaTeX document with required packages (algorithm, algpseudocodex, amsmath)
|
||||
- Includes both global and inline macros
|
||||
|
||||
**src/theme.ts**
|
||||
- MutationObserver watches document.body for theme changes
|
||||
- Applies Obsidian theme colors (--background-primary, --text-normal) to pseudocode blocks
|
||||
- Updates .ps-root, .ps-algorithm, and border colors dynamically
|
||||
|
||||
**src/setting_tab.ts**
|
||||
- UI for plugin settings (block size, preamble path, line numbers, theme following, etc.)
|
||||
|
||||
**src/auto_complete.ts**
|
||||
- EditorSuggestor for autocomplete within `pseudo` code blocks
|
||||
|
||||
### Build System
|
||||
- Uses esbuild (config in `esbuild.config.mjs`)
|
||||
- Bundles to CommonJS format targeting ES2018
|
||||
- Main entry: `main.ts` → `main.js`
|
||||
- External: Obsidian API, CodeMirror 6, Electron
|
||||
|
||||
## Important Implementation Details
|
||||
|
||||
### Preamble System
|
||||
- Global preamble: loaded from file (default `preamble.sty`)
|
||||
- Inline preamble: defined per-block before `\begin{algorithm}`
|
||||
- Preamble is injected into every math expression in the pseudocode
|
||||
- Must reload plugin after changing global preamble file
|
||||
|
||||
### Theme Integration
|
||||
- When `followSystemTheme` is enabled, plugin observes Obsidian theme changes
|
||||
- CSS custom properties are read from document.body and applied to rendered pseudocode
|
||||
- Observer must be detached on plugin unload to prevent memory leaks
|
||||
|
||||
### Error Handling
|
||||
- Pseudocode rendering errors display in-block with ✖ symbol
|
||||
- Preamble loading failures show Notice to user
|
||||
- Invalid LaTeX in preamble is caught and logged to console
|
||||
|
||||
## Dependencies
|
||||
- `pseudocode`: Uses forked version from ytliu74/pseudocode.js#master
|
||||
- `katex`: Fixed at 0.11.1 for macro rendering
|
||||
- `obsidian`: Latest API
|
||||
- TypeScript 4.7.4 with strict null checks enabled
|
||||
46
README.md
46
README.md
|
|
@ -3,13 +3,11 @@
|
|||
- [Features](#features)
|
||||
- [Future Features](#future-features)
|
||||
- [Usage](#usage)
|
||||
- [Basic](#basic)
|
||||
- [Preamble style customization](#preamble-style-customization)
|
||||
- [Use a `.sty` file](#use-a-sty-file)
|
||||
- [Use in-block preamble](#use-in-block-preamble)
|
||||
- [Supported macros](#supported-macros)
|
||||
- [Export to a compilable LaTeX file](#export-to-a-compilable-latex-file)
|
||||
- [Installation](#installation)
|
||||
- [Install from the Community Plugins in Obsidian.](#install-from-the-community-plugins-in-obsidian)
|
||||
- [Use BRAT](#use-brat)
|
||||
- [Manual install](#manual-install)
|
||||
- [Credits](#credits)
|
||||
|
||||
This is a plugin for [Obsidian](https://obsidian.md/) that allows you to render LaTeX-style pseudocode inside a code block. The plugin is based on [pseudocode.js](https://github.com/SaswatPadhi/pseudocode.js), a JavaScript library that typesets pseudocode beautifully to HTML.
|
||||
|
|
@ -20,18 +18,16 @@ This is a plugin for [Obsidian](https://obsidian.md/) that allows you to render
|
|||
- Print quality: The HTML output produced by the plugin is (almost) identical with the pretty algorithms printed on publications that are typeset by LaTeX.
|
||||
- Math formula support: Inserting math formulas in the pseudocode is as easy as LaTeX. Just enclose math expression in `$...$` or `\(...\)`.
|
||||
- Auto-completion inside `pseudo` code block. (Release 1.1.0)
|
||||
- [Preamble style (macros) customization.](#preamble-style-customization) (Release 1.2.0 & 1.5.0)
|
||||
- [Export a LaTeX file that can be compiled, including any required additional macros.](#export-to-a-compilable-latex-file) (Release 1.3.0)
|
||||
- Pseudocode block follows Obsidian theme and supports both light and dark ones. (Release 1.6.0)
|
||||
- Preamble style (macros) customization. (Release 1.2.0)
|
||||
|
||||
### Future Features
|
||||
|
||||
- [ ] More macros support.
|
||||
- [ ] Generate a LaTeX file that can be compiled, including any required additional macros.
|
||||
- [ ] Syntax highlighting.
|
||||
|
||||
## Usage
|
||||
|
||||
### Basic
|
||||
|
||||
To use the plugin, simply create a code block in your Obsidian note and add your pseudocode inside it. Then, add the language specifier `pseudo` (short for "pseudocode") to the code block. The plugin will automatically render the pseudocode as LaTeX.
|
||||
|
||||
**Rocommend: use the command `Pseudocode: Insert a new pseudocode block` to start.**
|
||||
|
|
@ -67,7 +63,7 @@ Here is an example:
|
|||
```
|
||||
```
|
||||
|
||||
This will be rendered as (to render line number, you need to toggle it in setting tab):
|
||||
This will be rendered as:
|
||||
|
||||
<div align="center">
|
||||
<img src="assets/example.png" alt="example" width="70%">
|
||||
|
|
@ -75,37 +71,17 @@ This will be rendered as (to render line number, you need to toggle it in settin
|
|||
|
||||
### Preamble style customization
|
||||
|
||||
#### Use a `.sty` file
|
||||
|
||||
You can use a `.sty` file (actually the suffix does not matter) to customize with some macros. The plugin will locate the file according to the setting. The default path is `preamble.sty`.
|
||||
You can use a `.sty` file (actually the suffix does not matter) to customize with some macros. The plugin will locate the file according to the setting. The default path is `preamble.sty`. Currently supported macros can be found at [this link](https://katex.org/docs/supported.html#macros). More macros will be supported in the future.
|
||||
|
||||
Please reload the plugin after you change the preamble file.
|
||||
|
||||
#### Use in-block preamble
|
||||
|
||||
You can simply write your own macros in the pseudocode block before `\begin{algorithm}`. These macros will only be applicable within this specific block.
|
||||
|
||||
#### Supported macros
|
||||
|
||||
Currently supported macros can be found at [this link](https://katex.org/docs/supported.html#macros) and below(might not be fully supported):
|
||||
|
||||
1. `\DeclarePairedDelimiter`
|
||||
2. `\DeclareMathOperator*`
|
||||
3. `\DeclareMathOperator`
|
||||
|
||||
|
||||
### Export to a compilable LaTeX file
|
||||
|
||||
You can easily export a compilable LaTeX file by clicking the `Export to clipboard` button at the bottom right corner for each pseudocode block. The plugin will automatically generate a compilable LaTeX file, including any required additional macros, to your clipboard.
|
||||
|
||||
|
||||
## Installation
|
||||
|
||||
<!-- ### Install from the Community Plugins in Obsidian. -->
|
||||
### Install from the Community Plugins in Obsidian.
|
||||
|
||||
:tada: The Pseudocode plugin is now available in the Community Plugins section of Obsidian. To install it, simply search for **Pseudocode** and click on the installation button.
|
||||
|
||||
<!-- ### Use [BRAT](https://github.com/TfTHacker/obsidian42-brat#Quick-Guide-for-using-BRAT)
|
||||
### Use [BRAT](https://github.com/TfTHacker/obsidian42-brat#Quick-Guide-for-using-BRAT)
|
||||
|
||||
1. Install **Obsidian-42 BRAT** from the Community Plugins in Obsidian.
|
||||
2. Open the command palette and run the command `BRAT: Add a beta plugin for testing`. Input this repo's URL `https://github.com/Yaotian-Liu/obsidian-pseudocode`.
|
||||
|
|
@ -118,7 +94,7 @@ You can easily export a compilable LaTeX file by clicking the `Export to clipboa
|
|||
1. Create a folder named `pseudocode-in-obs` in your Obsidian vault plugin folder (which is {Your Vault}/.obsidian/plugins).
|
||||
2. Download `main.js`, `manifest.json` and `styles.css` from the [releases page](https://github.com/yaotian-liu/obsidian-pseudocode/releases/latest), to the folder you just created in step 1.
|
||||
3. Open your Obsidian, and enable the plugin in "Community Plugins" setting page.
|
||||
4. Enjoy. -->
|
||||
4. Enjoy.
|
||||
|
||||
<!-- ## Known Issues -->
|
||||
|
||||
|
|
|
|||
45
main.ts
45
main.ts
|
|
@ -12,15 +12,12 @@ import {
|
|||
translateUnsupportedMacrosPerf,
|
||||
checkTranslatedMacros,
|
||||
} from "src/latex_translator";
|
||||
import { createExportButton } from "src/export_button";
|
||||
import { extractInlineMacros } from "src/inline_macro";
|
||||
import { setObserver, detachObserver, setPseudocodeTheme } from "src/theme";
|
||||
|
||||
import * as pseudocode from "pseudocode";
|
||||
|
||||
export default class PseudocodePlugin extends Plugin {
|
||||
settings: PseudocodeSettings;
|
||||
preamble: string = "";
|
||||
preamble: string;
|
||||
|
||||
async pseudocodeHandler(
|
||||
source: string,
|
||||
|
|
@ -30,35 +27,18 @@ export default class PseudocodePlugin extends Plugin {
|
|||
const blockDiv = el.createDiv({ cls: "pseudocode-block" });
|
||||
const blockWidth = this.settings.blockSize;
|
||||
blockDiv.style.width = `${blockWidth}em`;
|
||||
blockDiv.addEventListener("click", (event) => {
|
||||
const target = (event.target as HTMLElement | null);
|
||||
if(target && target.tagName !== 'BUTTON')
|
||||
(target
|
||||
.closest('.pseudocode-block')
|
||||
?.parentElement?.parentElement
|
||||
?.querySelector('.edit-block-button') as HTMLElement | null)
|
||||
?.click();
|
||||
});
|
||||
|
||||
// Extract inline macros
|
||||
const [inlineMacros, nonMacroLines] = extractInlineMacros(source);
|
||||
const allPreamble = this.preamble + inlineMacros;
|
||||
|
||||
// find all $ enclosements in source, and add the preamble.
|
||||
// TODO: Might be able to optimize.
|
||||
const mathRegex = /\$(.*?)\$/g;
|
||||
const withPreamble = nonMacroLines.replace(mathRegex, (_, group1) => {
|
||||
return `$${allPreamble}${group1}$`;
|
||||
source = source.replace(mathRegex, (match, group1) => {
|
||||
return "$" + this.preamble + group1 + "$";
|
||||
});
|
||||
|
||||
const preEl = blockDiv.createEl("pre", {
|
||||
cls: "code",
|
||||
text: withPreamble,
|
||||
});
|
||||
const preEl = blockDiv.createEl("pre", { cls: "code", text: source });
|
||||
|
||||
try {
|
||||
pseudocode.renderElement(preEl, this.settings.jsSettings);
|
||||
createExportButton(this, blockDiv, inlineMacros, nonMacroLines);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
const errorSpan = blockDiv.createEl("span", {
|
||||
|
|
@ -68,22 +48,11 @@ export default class PseudocodePlugin extends Plugin {
|
|||
blockDiv.empty();
|
||||
blockDiv.appendChild(errorSpan);
|
||||
}
|
||||
|
||||
// Set the pseudocode theme
|
||||
if (this.settings.followSystemTheme) {
|
||||
setPseudocodeTheme(blockDiv);
|
||||
}
|
||||
}
|
||||
|
||||
async onload() {
|
||||
await this.loadSettings();
|
||||
|
||||
setObserver();
|
||||
|
||||
if (this.settings.preambleEnabled) {
|
||||
console.log("Preamble is enabled.");
|
||||
await this.loadPreamble();
|
||||
}
|
||||
await this.loadPreamble();
|
||||
|
||||
this.registerMarkdownCodeBlockProcessor(
|
||||
BLOCK_NAME,
|
||||
|
|
@ -106,9 +75,7 @@ export default class PseudocodePlugin extends Plugin {
|
|||
});
|
||||
}
|
||||
|
||||
onunload() {
|
||||
detachObserver();
|
||||
}
|
||||
onunload() {}
|
||||
|
||||
async loadPreamble() {
|
||||
try {
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
{
|
||||
"id": "pseudocode-in-obs",
|
||||
"name": "Pseudocode",
|
||||
"version": "1.6.5",
|
||||
"version": "1.2.3",
|
||||
"minAppVersion": "0.15.0",
|
||||
"description": "This is an obsidian plugin that helps to render a LaTeX-style pseudocode inside a code block.",
|
||||
"author": "Yaotian Liu",
|
||||
"authorUrl": "https://github.com/ytliu74",
|
||||
"authorUrl": "https://github.com/Yaotian-Liu",
|
||||
"isDesktopOnly": false
|
||||
}
|
||||
10
package-lock.json
generated
10
package-lock.json
generated
|
|
@ -1,16 +1,16 @@
|
|||
{
|
||||
"name": "pseudocode-in-obs",
|
||||
"version": "1.6.4",
|
||||
"version": "1.2.3",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "pseudocode-in-obs",
|
||||
"version": "1.6.4",
|
||||
"version": "1.2.3",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"katex": "0.11.1",
|
||||
"pseudocode": "github:ytliu74/pseudocode.js#master"
|
||||
"pseudocode": "Yaotian-Liu/pseudocode.js#master"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/katex": "0.11.1",
|
||||
|
|
@ -1887,8 +1887,8 @@
|
|||
}
|
||||
},
|
||||
"node_modules/pseudocode": {
|
||||
"version": "2.3.0",
|
||||
"resolved": "git+ssh://git@github.com/ytliu74/pseudocode.js.git#db17188d06528b12b3279f4f1025c1a978519d1e",
|
||||
"version": "2.1.1",
|
||||
"resolved": "git+ssh://git@github.com/Yaotian-Liu/pseudocode.js.git#040c7ca05af0dc3e568d9b2831bf17f4fd00da8f",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/punycode": {
|
||||
|
|
|
|||
12
package.json
12
package.json
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "pseudocode-in-obs",
|
||||
"version": "1.6.5",
|
||||
"version": "1.2.3",
|
||||
"description": "This is an obsidian plugin that helps to render a LaTeX-style pseudocode inside a code block.",
|
||||
"main": "main.js",
|
||||
"scripts": {
|
||||
|
|
@ -12,7 +12,6 @@
|
|||
"author": "",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@types/katex": "0.11.1",
|
||||
"@types/node": "^16.11.6",
|
||||
"@typescript-eslint/eslint-plugin": "5.29.0",
|
||||
"@typescript-eslint/parser": "5.29.0",
|
||||
|
|
@ -20,10 +19,11 @@
|
|||
"esbuild": "0.17.3",
|
||||
"obsidian": "latest",
|
||||
"tslib": "2.4.0",
|
||||
"typescript": "4.7.4"
|
||||
"typescript": "4.7.4",
|
||||
"@types/katex": "0.11.1"
|
||||
},
|
||||
"dependencies": {
|
||||
"katex": "0.11.1",
|
||||
"pseudocode": "github:ytliu74/pseudocode.js#master"
|
||||
"pseudocode": "Yaotian-Liu/pseudocode.js#master",
|
||||
"katex": "0.11.1"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -81,16 +81,7 @@ export class PseudocodeSuggestor extends EditorSuggest<string> {
|
|||
const start = this.context.start;
|
||||
const end = editor.getCursor();
|
||||
|
||||
const pairSuggestion = this.pairSuggestions[suggestion];
|
||||
let insertText = suggestion;
|
||||
if (pairSuggestion) {
|
||||
const line = editor.getLine(start.line);
|
||||
const indentMatch = line.match(/^(\s*)/)?.[0] ?? "";
|
||||
const indent = indentMatch.replace(/\t/g, " "); // replace each tab with four spaces
|
||||
insertText += "\n" + indent + pairSuggestion;
|
||||
}
|
||||
|
||||
editor.replaceRange(insertText, start, end);
|
||||
editor.replaceRange(suggestion, start, end);
|
||||
const newCursor = end;
|
||||
newCursor.ch = start.ch + suggestion.length;
|
||||
|
||||
|
|
@ -100,19 +91,6 @@ export class PseudocodeSuggestor extends EditorSuggest<string> {
|
|||
}
|
||||
}
|
||||
|
||||
private pairSuggestions: Record<string, string> = {
|
||||
"\\begin{algorithmic}": "\\end{algorithmic}",
|
||||
"\\begin{algorithm}": "\\end{algorithm}",
|
||||
"\\Procedure{}{}": "\\EndProcedure",
|
||||
"\\Function{}{}": "\\EndFunction",
|
||||
"\\For{}": "\\EndFor",
|
||||
"\\ForAll{}": "\\EndFor",
|
||||
"\\If{}": "\\EndIf",
|
||||
"\\While{}": "\\EndWhile",
|
||||
"\\Repeat": "\\Until{}",
|
||||
// Add more pairs as needed
|
||||
};
|
||||
|
||||
private pseudocodeKeywords: string[] = [
|
||||
"\\begin{algorithmic}",
|
||||
"\\begin{algorithm}",
|
||||
|
|
@ -131,17 +109,13 @@ export class PseudocodeSuggestor extends EditorSuggest<string> {
|
|||
"\\Return",
|
||||
"\\Print",
|
||||
"\\For{}",
|
||||
"\\ForAll{}",
|
||||
"\\EndFor",
|
||||
"\\If{}",
|
||||
"\\Else",
|
||||
"\\Elif{}",
|
||||
"\\EndIf",
|
||||
"\\While{}",
|
||||
"\\EndWhile",
|
||||
"\\Repeat",
|
||||
"\\Continue",
|
||||
"\\Break",
|
||||
"\\Until{}",
|
||||
"\\Comment{}",
|
||||
"\\{",
|
||||
|
|
|
|||
|
|
@ -1,90 +0,0 @@
|
|||
const BUTTON_INFO = "Export to clipboard";
|
||||
const BUTTON_EXPORTED = "Exported!";
|
||||
const BUTTON_FAILED = "Failed to export";
|
||||
|
||||
import PseudocodePlugin from "main";
|
||||
|
||||
export function createExportButton(
|
||||
parentPlugin: PseudocodePlugin,
|
||||
parentDiv: HTMLDivElement,
|
||||
inlineMacros: string,
|
||||
blockContent: string
|
||||
) {
|
||||
const button = parentDiv.createEl("button");
|
||||
button.classList.add("hover-button");
|
||||
button.textContent = BUTTON_INFO;
|
||||
button.addEventListener("click", () => {
|
||||
console.log(BUTTON_INFO);
|
||||
if (blockContent !== null) {
|
||||
const exportContent =
|
||||
"\\documentclass{article}\n" +
|
||||
macros(parentPlugin, inlineMacros) +
|
||||
"\n" +
|
||||
"\\begin{document}\n" +
|
||||
processBlock(blockContent, parentPlugin) +
|
||||
"\n\\end{document}";
|
||||
|
||||
navigator.clipboard
|
||||
.writeText(exportContent)
|
||||
.then(() => {
|
||||
console.log("Copied to clipboard");
|
||||
button.textContent = BUTTON_EXPORTED;
|
||||
|
||||
setTimeout(() => {
|
||||
button.textContent = BUTTON_INFO;
|
||||
}, 3000);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Failed to copy to clipboard: ", error);
|
||||
button.textContent = BUTTON_FAILED;
|
||||
});
|
||||
}
|
||||
});
|
||||
button.addEventListener("mouseover", () => {
|
||||
button.textContent = BUTTON_INFO;
|
||||
});
|
||||
}
|
||||
|
||||
const macros = (parentPlugin: PseudocodePlugin, inlineMacros: string): string => {
|
||||
const noEnd = parentPlugin.settings.jsSettings.noEnd;
|
||||
const scopeLines = parentPlugin.settings.jsSettings.scopeLines;
|
||||
|
||||
// Split inline macros into lines and remove heading or trailing spaces
|
||||
const inlineMacrosLine = inlineMacros.split("\n").map(line => line.trim());
|
||||
|
||||
return `
|
||||
\\usepackage{algorithm}
|
||||
\\usepackage[noEnd=${noEnd},indLines=${scopeLines}]{algpseudocodex}
|
||||
|
||||
\\newcommand{\\And}{\\textbf{and~}}
|
||||
\\newcommand{\\Or}{\\textbf{or~}}
|
||||
\\newcommand{\\Xor}{\\textbf{xor~}}
|
||||
\\newcommand{\\Not}{\\textbf{not~}}
|
||||
\\newcommand{\\To}{\\textbf{to~}}
|
||||
\\newcommand{\\DownTo}{\\textbf{downto~}}
|
||||
\\newcommand{\\True}{\\textbf{true~}}
|
||||
\\newcommand{\\False}{\\textbf{false~}}
|
||||
\\newcommand{\\Input}{\\item[\\textbf{Input:}]}
|
||||
\\renewcommand{\\Output}{\\item[\\textbf{Output:}]}
|
||||
\\newcommand{\\Print}{\\State \\textbf{print~}}
|
||||
\\renewcommand{\\Return}{\\State \\textbf{return~}}
|
||||
|
||||
\\usepackage{amsmath}
|
||||
${inlineMacrosLine}
|
||||
`;
|
||||
};
|
||||
|
||||
const processBlock = (
|
||||
block: string,
|
||||
parentPlugin: PseudocodePlugin
|
||||
): string => {
|
||||
if (parentPlugin.settings.jsSettings.lineNumber)
|
||||
// Replace "\begin{algorithmic}" with "\begin{algorithmic}[1]"
|
||||
block = block.replace(
|
||||
"\\begin{algorithmic}",
|
||||
"\\begin{algorithmic}[1]"
|
||||
);
|
||||
else;
|
||||
|
||||
return block;
|
||||
};
|
||||
|
|
@ -1,24 +0,0 @@
|
|||
import {
|
||||
translateUnsupportedMacrosPerf,
|
||||
checkTranslatedMacros,
|
||||
} from "./latex_translator";
|
||||
|
||||
export function extractInlineMacros(source: string): [string, string] {
|
||||
const sourceLines = source.split("\n");
|
||||
const macroStartIndex = sourceLines.findIndex(line => line.includes("\\begin{algorithm}"));
|
||||
|
||||
const macroLines = sourceLines.slice(0, macroStartIndex).join("\n");
|
||||
const nonMacroLines = sourceLines.slice(macroStartIndex).join("\n");
|
||||
|
||||
let inlineMacros = "";
|
||||
|
||||
try {
|
||||
const translated = translateUnsupportedMacrosPerf(macroLines);
|
||||
inlineMacros = checkTranslatedMacros(translated);
|
||||
} catch (error) {
|
||||
|
||||
console.error(error);
|
||||
}
|
||||
|
||||
return [inlineMacros, nonMacroLines];
|
||||
}
|
||||
|
|
@ -1,45 +1,39 @@
|
|||
// This is the setting for pseudocode.js
|
||||
export interface PseudocodeJsSettings {
|
||||
indentSize: string;
|
||||
commentDelimiter: string;
|
||||
lineNumber: boolean;
|
||||
lineNumberPunc: string;
|
||||
noEnd: boolean;
|
||||
captionCount: undefined;
|
||||
scopeLines: boolean;
|
||||
indentSize: string;
|
||||
commentDelimiter: string;
|
||||
lineNumber: boolean;
|
||||
lineNumberPunc: string;
|
||||
noEnd: boolean;
|
||||
captionCount: undefined;
|
||||
}
|
||||
|
||||
// Setting for this plugin
|
||||
export interface PseudocodeSettings {
|
||||
blockSize: number;
|
||||
preambleEnabled: boolean;
|
||||
preamblePath: string;
|
||||
preambleLoadedNotice: boolean;
|
||||
followSystemTheme: boolean;
|
||||
jsSettings: PseudocodeJsSettings;
|
||||
blockSize: number;
|
||||
preamblePath: string;
|
||||
preambleLoadedNotice: boolean;
|
||||
jsSettings: PseudocodeJsSettings;
|
||||
}
|
||||
|
||||
export const DEFAULT_SETTINGS: PseudocodeSettings = {
|
||||
blockSize: 99,
|
||||
preambleEnabled: false,
|
||||
preamblePath: "preamble.sty",
|
||||
preambleLoadedNotice: false,
|
||||
followSystemTheme: false,
|
||||
jsSettings: {
|
||||
indentSize: "1.2em",
|
||||
commentDelimiter: "//",
|
||||
lineNumber: false,
|
||||
lineNumberPunc: ":",
|
||||
noEnd: false,
|
||||
captionCount: undefined,
|
||||
scopeLines: false,
|
||||
},
|
||||
blockSize: 99,
|
||||
preamblePath: "preamble.sty",
|
||||
preambleLoadedNotice: false,
|
||||
jsSettings: {
|
||||
indentSize: "1.2em",
|
||||
commentDelimiter: "//",
|
||||
lineNumber: false,
|
||||
lineNumberPunc: ":",
|
||||
noEnd: false,
|
||||
captionCount: undefined,
|
||||
}
|
||||
};
|
||||
|
||||
export const PseudocodeBlockInit =
|
||||
"```pseudo\n" +
|
||||
"\t\\begin{algorithm}\n\t\\caption{Algo Caption}\n\t\\begin{algorithmic}" +
|
||||
"\n\n\t\\end{algorithmic}\n\t\\end{algorithm}" +
|
||||
"\n```";
|
||||
"```pseudo\n" +
|
||||
"\t\\begin{algorithm}\n\t\\caption{Algo Caption}\n\t\\begin{algorithmic}" +
|
||||
"\n\n\t\\end{algorithmic}\n\t\\end{algorithm}" +
|
||||
"\n```";
|
||||
|
||||
export const BLOCK_NAME = "pseudo";
|
||||
export const BLOCK_NAME = "pseudo";
|
||||
|
|
@ -1,5 +1,8 @@
|
|||
import { App, PluginSettingTab, Setting } from "obsidian";
|
||||
import { setObserver, detachObserver } from "./theme";
|
||||
import {
|
||||
App,
|
||||
PluginSettingTab,
|
||||
Setting,
|
||||
} from "obsidian";
|
||||
|
||||
import PseudocodePlugin from "main";
|
||||
|
||||
|
|
@ -25,8 +28,8 @@ export class PseudocodeSettingTab extends PluginSettingTab {
|
|||
.setName("Block Size")
|
||||
.setDesc(
|
||||
"The width of the pseudocode block. The unit is 'em'." +
|
||||
" The default value is 99, which will work as the max width of the editor." +
|
||||
" '30' looks good for me."
|
||||
" The default value is 99, which will work as the max width of the editor." +
|
||||
" '30' looks good for me."
|
||||
)
|
||||
.addText((text) =>
|
||||
text
|
||||
|
|
@ -37,24 +40,6 @@ export class PseudocodeSettingTab extends PluginSettingTab {
|
|||
})
|
||||
);
|
||||
|
||||
// Instantiate Follow System Theme setting
|
||||
new Setting(containerEl)
|
||||
.setName("Follow System Theme")
|
||||
.setDesc("Whether to follow the system theme.")
|
||||
.addToggle((toggle) =>
|
||||
toggle
|
||||
.setValue(this.plugin.settings.followSystemTheme)
|
||||
.onChange(async (value) => {
|
||||
this.plugin.settings.followSystemTheme = value;
|
||||
if (value) {
|
||||
setObserver();
|
||||
} else {
|
||||
detachObserver();
|
||||
}
|
||||
await this.plugin.saveSettings();
|
||||
})
|
||||
);
|
||||
|
||||
// Instantiate Indent Size setting
|
||||
new Setting(containerEl)
|
||||
.setName("Indent Size")
|
||||
|
|
@ -78,8 +63,7 @@ export class PseudocodeSettingTab extends PluginSettingTab {
|
|||
text
|
||||
.setValue(this.plugin.settings.jsSettings.commentDelimiter)
|
||||
.onChange(async (value) => {
|
||||
this.plugin.settings.jsSettings.commentDelimiter =
|
||||
value;
|
||||
this.plugin.settings.jsSettings.commentDelimiter = value;
|
||||
await this.plugin.saveSettings();
|
||||
})
|
||||
);
|
||||
|
|
@ -127,38 +111,8 @@ export class PseudocodeSettingTab extends PluginSettingTab {
|
|||
})
|
||||
);
|
||||
|
||||
// Instantiate scope lines toggle setting
|
||||
new Setting(containerEl)
|
||||
.setName("Scope Lines")
|
||||
.setDesc(
|
||||
"If enabled, pseudocode blocks will have lines indicating the scope of the block."
|
||||
)
|
||||
.addToggle((toggle) =>
|
||||
toggle
|
||||
.setValue(this.plugin.settings.jsSettings.scopeLines)
|
||||
.onChange(async (value) => {
|
||||
this.plugin.settings.jsSettings.scopeLines = value;
|
||||
await this.plugin.saveSettings();
|
||||
})
|
||||
);
|
||||
|
||||
containerEl.createEl("h2", { text: "Preamble Settings" });
|
||||
|
||||
// Instantiate Preamble Enabled setting
|
||||
new Setting(containerEl)
|
||||
.setName("Preamble Enabled")
|
||||
.setDesc(
|
||||
"Whether to load the preamble file. Please reload the plugin for this setting to take effect."
|
||||
)
|
||||
.addToggle((toggle) =>
|
||||
toggle
|
||||
.setValue(this.plugin.settings.preambleEnabled)
|
||||
.onChange(async (value) => {
|
||||
this.plugin.settings.preambleEnabled = value;
|
||||
await this.plugin.saveSettings();
|
||||
})
|
||||
);
|
||||
|
||||
// Instantiate Preamble Path setting
|
||||
new Setting(containerEl)
|
||||
.setName("Preamble Path")
|
||||
|
|
@ -178,9 +132,7 @@ export class PseudocodeSettingTab extends PluginSettingTab {
|
|||
// Instantiate Preamble Load Sign setting
|
||||
new Setting(containerEl)
|
||||
.setName("Preamble Loaded Notice")
|
||||
.setDesc(
|
||||
"Whether to show a notice everytime the preamble is loaded."
|
||||
)
|
||||
.setDesc("Whether to show a notice everytime the preamble is loaded.")
|
||||
.addToggle((toggle) =>
|
||||
toggle
|
||||
.setValue(this.plugin.settings.preambleLoadedNotice)
|
||||
|
|
@ -190,4 +142,4 @@ export class PseudocodeSettingTab extends PluginSettingTab {
|
|||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
77
src/theme.ts
77
src/theme.ts
|
|
@ -1,77 +0,0 @@
|
|||
// Reference: https://forum.obsidian.md/t/obsidian-publish-api-how-to-track-changing-theme/73637/4
|
||||
|
||||
export const themeObserver = new MutationObserver(function (mutations) {
|
||||
mutations.forEach(function (mutation) {
|
||||
const target = mutation.target as HTMLElement;
|
||||
if (
|
||||
// dark -> dark & light -> light
|
||||
mutation.oldValue?.contains("theme-dark") &&
|
||||
!mutation.oldValue?.contains("theme-light") && // key line, avoid calling twice
|
||||
target.classList.value.contains("theme-light")
|
||||
) {
|
||||
console.log("light theme detected");
|
||||
setPseudocodeTheme();
|
||||
} else if (
|
||||
// light -> empty -> dark
|
||||
mutation.oldValue?.contains("theme-light") && // key line, avoid calling twice
|
||||
!mutation.oldValue?.contains("theme-dark") &&
|
||||
target.classList.value.contains("theme-dark")
|
||||
) {
|
||||
console.log("dark theme detected");
|
||||
setPseudocodeTheme();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
export function setPseudocodeTheme(psBlock?: Element) {
|
||||
|
||||
const bodyElement = document.body;
|
||||
const backgroundValue = getComputedStyle(bodyElement)
|
||||
.getPropertyValue("--background-primary")
|
||||
.trim();
|
||||
const fontValue = getComputedStyle(bodyElement)
|
||||
.getPropertyValue("--text-normal")
|
||||
.trim();
|
||||
console.log(backgroundValue, fontValue);
|
||||
|
||||
const psRootElements = psBlock
|
||||
? psBlock.querySelectorAll(".ps-root")
|
||||
: document.querySelectorAll(".ps-root");
|
||||
// console.log(psRootElements);
|
||||
|
||||
// Loop through each element and modify the CSS properties
|
||||
psRootElements.forEach((element) => {
|
||||
const htmlElement = element as HTMLElement;
|
||||
htmlElement.style.backgroundColor = backgroundValue;
|
||||
htmlElement.style.opacity = "1";
|
||||
htmlElement.style.color = fontValue;
|
||||
|
||||
// Change border colors for .ps-algorithm and .ps-algorithm.with-caption > .ps-line:first-child
|
||||
const algorithmElements = htmlElement.querySelectorAll(".ps-algorithm");
|
||||
algorithmElements.forEach((algElement) => {
|
||||
const algHtmlElement = algElement as HTMLElement;
|
||||
algHtmlElement.style.borderTopColor = fontValue;
|
||||
algHtmlElement.style.borderBottomColor = fontValue;
|
||||
});
|
||||
|
||||
const lineElements = htmlElement.querySelectorAll(
|
||||
".ps-algorithm.with-caption > .ps-line:first-child"
|
||||
);
|
||||
lineElements.forEach((lineElement) => {
|
||||
const lineHtmlElement = lineElement as HTMLElement;
|
||||
lineHtmlElement.style.borderBottomColor = fontValue;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export const setObserver = () => {
|
||||
themeObserver.observe(document.body, {
|
||||
attributes: true,
|
||||
attributeOldValue: true,
|
||||
attributeFilter: ["class"],
|
||||
});
|
||||
};
|
||||
|
||||
export const detachObserver = () => {
|
||||
themeObserver.disconnect();
|
||||
};
|
||||
51
styles.css
51
styles.css
|
|
@ -1399,22 +1399,12 @@ If your plugin does not need CSS, delete this file.
|
|||
text-transform: none;
|
||||
}
|
||||
|
||||
.ps-root {
|
||||
font-family: KaTeX_Main, "Times New Roman", Times, serif;
|
||||
font-weight: 400;
|
||||
font-variant: normal;
|
||||
font-style: normal;
|
||||
text-transform: none;
|
||||
}
|
||||
|
||||
.ps-comment {
|
||||
font-family: KaTeX_Main, "Times New Roman", Times, serif;
|
||||
font-weight: 400;
|
||||
font-variant: normal;
|
||||
font-style: italic;
|
||||
text-transform: none;
|
||||
opacity: 70%;
|
||||
float: right;
|
||||
.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 {
|
||||
|
|
@ -1435,16 +1425,6 @@ If your plugin does not need CSS, delete this file.
|
|||
text-indent: 0;
|
||||
}
|
||||
|
||||
/* scope lines support */
|
||||
.ps-root .ps-algorithmic.with-scopelines div.ps-block {
|
||||
border-left-style: solid;
|
||||
border-left-width: 0.1em;
|
||||
padding-left: 0.6em;
|
||||
}
|
||||
.ps-root .ps-algorithmic.with-scopelines > div.ps-block {
|
||||
border-left: none;
|
||||
}
|
||||
|
||||
.error-message {
|
||||
font-family: var(--font-monospace);
|
||||
font-size: 0.875rem;
|
||||
|
|
@ -1457,25 +1437,8 @@ If your plugin does not need CSS, delete this file.
|
|||
}
|
||||
|
||||
.pseudocode-block {
|
||||
position: relative;
|
||||
max-width: 100%;
|
||||
box-sizing: border-box;
|
||||
margin: auto;
|
||||
}
|
||||
|
||||
.hover-button {
|
||||
position: absolute;
|
||||
right: 0;
|
||||
bottom: 0;
|
||||
display: none;
|
||||
}
|
||||
|
||||
.pseudocode-block:hover .hover-button {
|
||||
display: block;
|
||||
}
|
||||
|
||||
.ps-root {
|
||||
background-color: white;
|
||||
opacity: 1;
|
||||
color: black;
|
||||
}
|
||||
|
||||
|
|
@ -15,27 +15,5 @@
|
|||
"1.2.0": "0.15.0",
|
||||
"1.2.1": "0.15.0",
|
||||
"1.2.2": "0.15.0",
|
||||
"1.2.3": "0.15.0",
|
||||
"1.3.0": "0.15.0",
|
||||
"1.3.1": "0.15.0",
|
||||
"1.4.0": "0.15.0",
|
||||
"1.4.1": "0.15.0",
|
||||
"1.4.2": "0.15.0",
|
||||
"1.4.3": "0.15.0",
|
||||
"1.4.4": "0.15.0",
|
||||
"1.5.0": "0.15.0",
|
||||
"1.5.1": "0.15.0",
|
||||
"1.5.2": "0.15.0",
|
||||
"1.5.3": "0.15.0",
|
||||
"1.5.4": "0.15.0",
|
||||
"1.5.5": "0.15.0",
|
||||
"1.5.6": "0.15.0",
|
||||
"1.5.7": "0.15.0",
|
||||
"1.5.8": "0.15.0",
|
||||
"1.6.0": "0.15.0",
|
||||
"1.6.1": "0.15.0",
|
||||
"1.6.2": "0.15.0",
|
||||
"1.6.3": "0.15.0",
|
||||
"1.6.4": "0.15.0",
|
||||
"1.6.5": "0.15.0"
|
||||
"1.2.3": "0.15.0"
|
||||
}
|
||||
Loading…
Reference in a new issue