Skip to content

Commit

Permalink
Merge pull request #33 from 4very/main
Browse files Browse the repository at this point in the history
Add MD link option and GH link option (closes #30 and #8)
  • Loading branch information
hipstersmoothie authored May 11, 2023
2 parents 8e2ea8d + 6d51884 commit 2e183cd
Show file tree
Hide file tree
Showing 7 changed files with 1,109 additions and 788 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"@types/node": "^14.14.2",
"@typescript-eslint/eslint-plugin": "^4.15.1",
"@typescript-eslint/parser": "^4.15.1",
"anchor-markdown-header": "^0.7.0",
"auto": "^10.16.7",
"auto-plugin-obsidian": "^0.1.4",
"endent": "^2.0.1",
Expand Down
21 changes: 21 additions & 0 deletions src/anchor.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
declare module "anchor-markdown-header" {
/**
* @param header {String} The header to be anchored.
* @param mode {String} The anchor mode (github.com|nodejs.org|bitbucket.org|ghost.org|gitlab.com).
* @param repetition {Number} The nth occurrence of this header text, starting with 0. Not required for the 0th instance.
* @param moduleName {String} The name of the module of the given header (required only for 'nodejs.org' mode).
* @return {String} The header anchor that is compatible with the given mode.
*/
function anchor(
header: string,
mode?:
| "github.com"
| "nodejs.org"
| "bitbucket.org"
| "ghost.org"
| "gitlab.com",
repetition?: number,
moduleName?: string
): string;
export = anchor;
}
25 changes: 20 additions & 5 deletions src/create-toc.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import endent from "endent";
import { CachedMetadata, HeadingCache, Notice } from "obsidian";
import { TableOfContentsPluginSettings } from "./types";
import anchor from 'anchor-markdown-header';

export interface CursorPosition {
line: number;
Expand Down Expand Up @@ -77,11 +78,25 @@ export const createToc = (
.join("");
const previousLevelHeading = getPreviousLevelHeading(includedHeadings, heading);

if (typeof (previousLevelHeading) == "undefined") {
return `${indent}${itemIndication} [[#${heading.heading}|${heading.heading}]]`;
} else {
return `${indent}${itemIndication} [[#${previousLevelHeading.heading}#${heading.heading}|${heading.heading}]]`;
}
const prefix = `${indent}${itemIndication}`;
const displayText = heading.heading;
let linkText;

if (settings.useMarkdown && settings.githubCompat)
return `${prefix} ${anchor(heading.heading)}`;
else if (settings.useMarkdown)
linkText = encodeURI(heading.heading);
else if (typeof previousLevelHeading == "undefined")
linkText = heading.heading;
else
linkText = `${previousLevelHeading.heading}#${heading.heading}`;

// wikilink format
if (!settings.useMarkdown)
return `${prefix} [[#${linkText}|${displayText}]]`;
// markdown format
else
return `${prefix} [${displayText}](#${linkText})`;
});

return endent`
Expand Down
41 changes: 34 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import {
Plugin,
PluginSettingTab,
Setting,
ToggleComponent,
} from "obsidian";
import { createToc, getCurrentHeaderDepth } from "./create-toc";
import { TableOfContentsPluginSettings } from "./types";

export interface CursorPosition {
line: number;
Expand Down Expand Up @@ -86,6 +88,37 @@ class TableOfContentsSettingsTab extends PluginSettingTab {
this.plugin.saveData(this.plugin.settings);
})
);

new Setting(containerEl)
.setName("Use Markdown links")
.setDesc("Auto-generate Markdown links, instead of the default WikiLinks")
.addToggle((value) =>
value.setValue(this.plugin.settings.useMarkdown).onChange((value) => {
this.plugin.settings.useMarkdown = value;
this.plugin.saveData(this.plugin.settings);
if(!value) (githubSetting.components[0] as ToggleComponent).setValue(false)
githubSetting.setDisabled(!value)
})
);

const githubCompatDesc: DocumentFragment = new DocumentFragment()
githubCompatDesc.appendText("Github generates section links differently than Obsidian, this setting uses ")
githubCompatDesc.createEl('a', {href: "https://github.com/thlorenz/anchor-markdown-header", text: "anchor-markdown-header"})
githubCompatDesc.appendText(" to generate the proper links.")

const githubSetting = new Setting(containerEl)
.setName("Github compliant Markdown section links")
.setDesc(githubCompatDesc)
.setDisabled(!this.plugin.settings.useMarkdown)
.addToggle((value) =>
value
.setValue(this.plugin.settings.githubCompat ?? false)
.setDisabled(!this.plugin.settings.useMarkdown)
.onChange((value) => {
this.plugin.settings.githubCompat = value;
this.plugin.saveData(this.plugin.settings);
})
);
}
}

Expand All @@ -94,18 +127,12 @@ type GetSettings = (
cursor: CodeMirror.Position
) => TableOfContentsPluginSettings;

interface TableOfContentsPluginSettings {
listStyle: "bullet" | "number";
minimumDepth: number;
maximumDepth: number;
title?: string;
}

export default class TableOfContentsPlugin extends Plugin {
public settings: TableOfContentsPluginSettings = {
minimumDepth: 2,
maximumDepth: 6,
listStyle: "bullet",
useMarkdown: false
};

public async onload(): Promise<void> {
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ export interface TableOfContentsPluginSettings {
minimumDepth: number;
maximumDepth: number;
title?: string;
useMarkdown: boolean
githubCompat?: boolean
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"moduleResolution": "node",
"importHelpers": true,
"lib": ["dom", "es5", "scripthost", "es2015"],
"strict": true
"strict": true,
"types": ["./src/anchor.d.ts"]
},
"include": ["**/*.ts"]
}
Loading

0 comments on commit 2e183cd

Please sign in to comment.