This commit is contained in:
aaron-jack-manning 2022-11-19 17:13:42 +11:00 committed by Aaron Manning
commit 95078d234a
12 changed files with 2380 additions and 0 deletions

8
.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# npm
node_modules
# compiled files
main.js
# obsidian
data.json

7
LICENSE Executable file
View File

@ -0,0 +1,7 @@
Copyright © 2022 Aaron Manning
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

24
README.md Normal file
View File

@ -0,0 +1,24 @@
# Obsidian MathJax WikiLinks
Obsidian plugin to render MathJax within WikiLinks.
> **Note**:
> This plugin is no longer hosted on the community plugins list for Obsidian and I have no long term plans to maintain it, as it has exactly the functionality that I personally need.
>
> The functionality this plugin provided was merged into [obsidian-mathlinks](https://github.com/zhaoshenzhai/obsidian-mathlinks) upon release [release 0.3.0](https://github.com/zhaoshenzhai/obsidian-mathlinks/releases/tag/0.3.0), and [zhaoshenzhai](https://github.com/zhaoshenzhai) who developed that plugin has since added with added live preview support.
## Demo
In vanilla obsidian, the WikiLink `[[sigma-algebra|$\sigma$-algebra]]` renders without the MathJax expression as follows:
![No MathJax Link](no-mathjax-link.png)
while links of the form `[$\sigma$-algebra](sigma-algebra.md)` will render correctly:
![MathJax Link](mathjax-link.png)
This plugin unifies the two versions, rendering MathJax properly in the first case.
## Building From Source
```
> npm i
> npm run build
```

42
esbuild.config.mjs Normal file
View File

@ -0,0 +1,42 @@
import esbuild from "esbuild";
import process from "process";
import builtins from 'builtin-modules'
const banner =
`/*
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
if you want to view the source, please visit the github repository of this plugin
*/
`;
const prod = (process.argv[2] === 'production');
esbuild.build({
banner: {
js: banner,
},
entryPoints: ['main.ts'],
bundle: true,
external: [
'obsidian',
'electron',
'@codemirror/autocomplete',
'@codemirror/collab',
'@codemirror/commands',
'@codemirror/language',
'@codemirror/lint',
'@codemirror/search',
'@codemirror/state',
'@codemirror/view',
'@lezer/common',
'@lezer/highlight',
'@lezer/lr',
...builtins],
format: 'cjs',
watch: !prod,
target: 'es2018',
logLevel: "info",
sourcemap: prod ? false : 'inline',
treeShaking: true,
outfile: 'main.js',
}).catch(() => process.exit(1));

69
main.ts Normal file
View File

@ -0,0 +1,69 @@
import { Plugin, loadMathJax, finishRenderMath, renderMath } from 'obsidian';
function convertLink(link : HTMLElement) {
const linkText = link.textContent?.trim();
if (linkText?.contains('$')) {
// Each section of text between unescaped $ signs
// Fallback to empty list if null, as this happens when input is empty
const sections = mathSplit(linkText);
let isMath = false;
link.innerText = '';
for (let i = 0; i < sections.length; i++) {
if (isMath) {
link.createSpan().replaceWith(renderMath(sections[i], false));
} else {
link.createSpan().innerText += sections[i];
}
isMath = !isMath;
}
finishRenderMath();
}
}
// Splits at math delimeters, handling escapes properly
// Returns an array of alternating normal text and math text
function mathSplit(input : string) : string[] {
let output = [''];
let backslashes = 0;
for (let i = 0; i < input.length; i++) {
let curr = input[i];
if (curr == '\\') {
backslashes++;
}
else {
for (let j = 0; j < Math.ceil(backslashes / 2); j++) {
output[output.length - 1] = output[output.length - 1] + "\\";
}
if (curr == '$' && backslashes % 2 == 0) {
output.push('');
}
else {
output[output.length - 1] = output[output.length - 1] + curr;
}
backslashes = 0;
}
}
return output;
}
export default class MathJaxWikiLinks extends Plugin {
async onload() {
await loadMathJax();
this.registerMarkdownPostProcessor((element, context) => {
element.querySelectorAll('.internal-link').forEach(convertLink);
});
}
}

10
manifest.json Normal file
View File

@ -0,0 +1,10 @@
{
"id": "obsidian-mathjax-wikilinks",
"name": "MathJax WikiLinks",
"version": "0.0.1",
"minAppVersion": "0.15.0",
"description": "Allows rendering MathJax in WikiLink aliases.",
"author": "Aaron Manning",
"authorUrl": "https://aaronmanning.net",
"isDesktopOnly": false
}

BIN
mathjax-link.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

BIN
no-mathjax-link.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

2169
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

24
package.json Normal file
View File

@ -0,0 +1,24 @@
{
"name": "obsidian-mathjax-wikilinks",
"version": "0.0.1",
"description": "Allows rendering MathJax in WikiLink aliases. ",
"main": "main.js",
"scripts": {
"dev": "node esbuild.config.mjs",
"build": "tsc -noEmit -skipLibCheck && node esbuild.config.mjs production",
"version": "node version-bump.mjs && git add manifest.json versions.json"
},
"keywords": [],
"author": "",
"license": "MIT",
"devDependencies": {
"@types/node": "^16.11.6",
"@typescript-eslint/eslint-plugin": "5.29.0",
"@typescript-eslint/parser": "5.29.0",
"builtin-modules": "3.3.0",
"esbuild": "0.14.47",
"obsidian": "latest",
"tslib": "2.4.0",
"typescript": "4.7.4"
}
}

24
tsconfig.json Normal file
View File

@ -0,0 +1,24 @@
{
"compilerOptions": {
"baseUrl": ".",
"inlineSourceMap": true,
"inlineSources": true,
"module": "ESNext",
"target": "ES6",
"allowJs": true,
"noImplicitAny": true,
"moduleResolution": "node",
"importHelpers": true,
"isolatedModules": true,
"strictNullChecks": true,
"lib": [
"DOM",
"ES5",
"ES6",
"ES7"
]
},
"include": [
"**/*.ts"
]
}

3
versions.json Normal file
View File

@ -0,0 +1,3 @@
{
"0.0.1": "0.15.0"
}