Skip to content

Commit db87a3f

Browse files
committed
Initial commit
0 parents  commit db87a3f

File tree

9 files changed

+178
-0
lines changed

9 files changed

+178
-0
lines changed

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Intellij
2+
*.iml
3+
.idea
4+
5+
# npm
6+
node_modules
7+
package-lock.json
8+
9+
# build
10+
main.js
11+
*.js.map

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
## Obsidian Footnotes
2+
3+
Great idea by [jacob.4ristotle](https://forum.obsidian.md/u/jacob.4ristotle/summary) posted in [Footnote Shortcut](https://forum.obsidian.md/t/footnote-shortcut/8872).
4+
5+
> It would be convenient to have a shortcut to automate these steps. In particular, I envision that the shortcut would:
6+
> using the smallest natural number n that has not yet been used for a footnote,
7+
> * add `[^n]` at the insertion point
8+
> * add `[^n]: ` to the end of the note, and move the insertion point there.
9+
10+
That's what this does.
11+
12+
This plugin adds a new hot-key <kbd>Command</kbd>+<kbd>Shift</kbd>+<kbd>6</kbd> that when hit will insert a footnote with increased counter and move your cursor to the bottom of the file, add the other footnote pair and lets you type away...
13+
14+
("6" because this is based on a US keyboard where the uptick character "^" is the shift-value of the same key. Customize hot-key as needed.)
15+
16+
Let me know if you run into any issues!
17+
18+
Cheers,
19+
20+
Alexis

main.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import {App, MarkdownView, Modal, Plugin} from 'obsidian';
2+
3+
export default class MyPlugin extends Plugin {
4+
async onload() {
5+
this.addCommand({
6+
id: 'insert-footnote',
7+
name: 'Insert Footnote',
8+
checkCallback: (checking: boolean) => {
9+
this.insertFootnote(checking);
10+
},
11+
hotkeys: [
12+
{
13+
modifiers: ["Mod", "Shift"],
14+
key: "6",
15+
},
16+
]
17+
});
18+
}
19+
20+
insertFootnote(checking: boolean) {
21+
let leaf = this.app.workspace.activeLeaf;
22+
const mdView = leaf.view as MarkdownView;
23+
24+
if(mdView.sourceMode == undefined) return false;
25+
26+
const doc = mdView.sourceMode.cmEditor;
27+
28+
let editor = doc;
29+
let markdownText = mdView.data;
30+
let re = /\[\^(\d+)\]/gi;
31+
let matches = markdownText.match(re);
32+
let numbers: Array<number> = [];
33+
let currentMax = 1;
34+
35+
if(matches != null) {
36+
for(var i = 0; i <= matches.length - 1; i++){
37+
let match = matches[i];
38+
match = match.replace("[^", "");
39+
match = match.replace("]", "");
40+
let matchNumber = Number(match);
41+
numbers[i] = matchNumber;
42+
if(matchNumber + 1 > currentMax) {
43+
currentMax = matchNumber + 1;
44+
}
45+
}
46+
}
47+
48+
const cursorPosition = editor.getCursor();
49+
let lineText = editor.getLine(cursorPosition.line);
50+
let footNoteId = currentMax;
51+
let footnoteMarker = `[^${footNoteId}]`;
52+
let linePart1 = lineText.substr(0, cursorPosition.ch)
53+
let linePart2 = lineText.substr(cursorPosition.ch);
54+
let newLine = linePart1 + footnoteMarker + linePart2
55+
56+
editor.replaceRange(newLine, {line: cursorPosition.line, ch: 0}, {line: cursorPosition.line, ch: lineText.length})
57+
58+
let lastLine = editor.getLine(doc.lineCount() - 1);
59+
60+
if(lastLine.length > 0) {
61+
editor.replaceRange(`\n[^${footNoteId}]: `, {line: doc.lineCount(), ch: 0})
62+
} else {
63+
editor.replaceRange(`[^${footNoteId}]: `, {line: doc.lineCount(), ch: 0})
64+
}
65+
66+
editor.setCursor({line: doc.lineCount(), ch: 6});
67+
}
68+
}

manifest.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"id": "obsidian-footnotes",
3+
"name": "Footnotes",
4+
"version": "0.0.1",
5+
"minAppVersion": "0.9.12",
6+
"description": "Shortcut for adding footnotes",
7+
"author": "Alexis Rondeau",
8+
"authorUrl": "https://publish.obsidian.md/alexisrondeau",
9+
"isDesktopOnly": false
10+
}

package.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "obsidian-footnotes",
3+
"version": "0.0.1",
4+
"description": "",
5+
"main": "main.js",
6+
"scripts": {
7+
"dev": "rollup --config rollup.config.js -w",
8+
"build": "rollup --config rollup.config.js"
9+
},
10+
"keywords": [],
11+
"author": "",
12+
"license": "MIT",
13+
"devDependencies": {
14+
"@rollup/plugin-commonjs": "^15.1.0",
15+
"@rollup/plugin-node-resolve": "^9.0.0",
16+
"@rollup/plugin-typescript": "^6.0.0",
17+
"@types/node": "^14.14.2",
18+
"obsidian": "https://github.com/obsidianmd/obsidian-api/tarball/master",
19+
"rollup": "^2.32.1",
20+
"tslib": "^2.0.3",
21+
"typescript": "^4.0.3"
22+
}
23+
}

rollup.config.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import typescript from '@rollup/plugin-typescript';
2+
import {nodeResolve} from '@rollup/plugin-node-resolve';
3+
import commonjs from '@rollup/plugin-commonjs';
4+
5+
export default {
6+
input: 'main.ts',
7+
output: {
8+
dir: '.',
9+
sourcemap: 'inline',
10+
format: 'cjs',
11+
exports: 'default'
12+
},
13+
external: ['obsidian'],
14+
plugins: [
15+
typescript(),
16+
nodeResolve({browser: true}),
17+
commonjs(),
18+
]
19+
};

styles.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/* Sets all the text color to red! */

tsconfig.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"compilerOptions": {
3+
"baseUrl": ".",
4+
"inlineSourceMap": true,
5+
"inlineSources": true,
6+
"module": "ESNext",
7+
"target": "es6",
8+
"allowJs": true,
9+
"noImplicitAny": true,
10+
"moduleResolution": "node",
11+
"importHelpers": true,
12+
"lib": [
13+
"dom",
14+
"es5",
15+
"scripthost",
16+
"es2015"
17+
]
18+
},
19+
"include": [
20+
"**/*.ts"
21+
]
22+
}

versions.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"1.0.1": "0.9.12",
3+
"1.0.0": "0.9.7"
4+
}

0 commit comments

Comments
 (0)