Skip to content

Commit

Permalink
support Is there are reason we can't convert wikilinks to markdown li…
Browse files Browse the repository at this point in the history
…nks? #40
  • Loading branch information
Benature committed Mar 6, 2024
1 parent dcff7b4 commit 8bf3125
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 2.6.0

- [feature] Convert wikiLinks to plain markdown links in selection ([#40](https://github.com/Benature/obsidian-text-format/issues/40))
- [feature] remove trailing spaces ([#61](https://github.com/Benature/obsidian-text-format/issues/61))
- [updated] decodeURI
- `%2F` -> `/`
Expand Down
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,12 @@ Or you can consider to bind custom hotkeys to those commands according to [#29](

#### Links

| Command | Description |
| ------------------------------------------- | -------------------------------------------------- |
| Remove WikiLinks format in selection | Convert `[[WikiLinks]]` to `WikiLinks` (#28) |
| Remove URL links format in selection | Convert `[Google](www.google.com)` to `Google` |
| Convert URL links to WikiLinks in selection | Convert `[Google](www.google.com)` to `[[Google]]` |
| Command | Description |
| -------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------- |
| Remove WikiLinks format in selection | e.g. Convert `[[WikiLinks]]` to `WikiLinks` (#28) |
| Remove URL links format in selection | e.g. Convert `[Google](www.google.com)` to `Google` |
| Convert URL links to WikiLinks in selection | e.g. Convert `[Google](www.google.com)` to `[[Google]]` |
| Convert wikiLinks to plain markdown links in selection ⚙️ | e.g. Convert `[[Google]]` to `[Google](Google.md)` ([#40](https://github.com/Benature/obsidian-text-format/issues/40)) |

### PDF copy / OCR

Expand Down
14 changes: 12 additions & 2 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Editor, MarkdownView, Plugin, Notice, debounce } from "obsidian";
import { removeWikiLink, removeUrlLink, url2WikiLink } from "src/link";
import { removeWikiLink, removeUrlLink, url2WikiLink, convertWikiLinkToMarkdown } from "src/link";
import { FormatSettings, DEFAULT_SETTINGS, TextFormatSettingTab } from "src/setting";
import { array2markdown, table2bullet, capitalizeWord, capitalizeSentence, removeAllSpaces, zoteroNote, textWrapper, replaceLigature, ankiSelection, sortTodo, requestAPI, headingLevel, slugify, snakify, extraDoubleSpaces, toTitleCase } from "src/format";

Expand Down Expand Up @@ -162,6 +162,13 @@ export default class TextFormat extends Plugin {
this.textFormat(editor, view, "link-url2wiki");
},
});
this.addCommand({
id: "link-wiki2md",
name: { en: "Convert wikiLinks to plain markdown links in selection", zh: "将选中文本中的 WikiLinks 转换为普通 Markdown 链接格式", "zh-TW": "將選取文字中的 WikiLinks 轉換為普通 Markdown 鏈接格式" }[lang],
editorCallback: (editor: Editor, view: MarkdownView) => {
this.textFormat(editor, view, "link-wiki2md");
},
});


this.addCommand({
Expand Down Expand Up @@ -311,7 +318,6 @@ export default class TextFormat extends Plugin {
this.textFormat(editor, view, "space-word-symbol");
},
});
console.log(this)
}

updateCommandWrapper() {
Expand Down Expand Up @@ -616,6 +622,9 @@ export default class TextFormat extends Plugin {
case "link-url2wiki":
replacedText = url2WikiLink(selectedText);
break;
case "link-wiki2md":
replacedText = convertWikiLinkToMarkdown(selectedText, this);
break;
case "ligature":
replacedText = replaceLigature(selectedText);
break;
Expand All @@ -638,6 +647,7 @@ export default class TextFormat extends Plugin {
return;
})
return;

default:
return;
}
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian-text-format",
"name": "Text Format",
"version": "2.5.1",
"version": "2.6.0",
"minAppVersion": "0.9.7",
"description": "Format text such as lowercase/uppercase/capitalize/titlecase, converting order/bullet list, removing redundant spaces/newline characters.",
"author": "Benature",
Expand Down
24 changes: 24 additions & 0 deletions src/link.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { stringFormat } from "./format";
import { WikiLinkFormatGroup } from "./setting";
import TextFormat from "../main";

export function removeWikiLink(s: string, formatGroup: WikiLinkFormatGroup): string {
return s.replace(/\[\[.*?\]\]/g, function (t) {
Expand Down Expand Up @@ -46,3 +47,26 @@ export function url2WikiLink(s: string): string {
return `[[${t.match(/\[(.*?)\]/)[1]}]]`;
});
}

export function convertWikiLinkToMarkdown(wikiLink: string, plugin: TextFormat): string {
const regex = /\[\[([^|\]]+)\|?([^\]]+)?\]\]/g;

const markdown = wikiLink.replace(regex, (match, p1, p2) => {
const linkText = p2 ? p2.trim() : p1.trim();
let linkTarget = p1.trim().replace(/#.*$/g, "") + ".md";

const note = plugin.app.vault.getAllLoadedFiles().find(file => file.name === linkTarget);
if (note) {
linkTarget = note.path.replace(/\s/g, "%20");
if (!plugin.settings.isWikiLink2mdRelativePath) {
// @ts-ignore
linkTarget = plugin.app.vault.adapter.basePath + "/" + linkTarget;
}
} else {
// return wikiLink;
}
return `[${linkText}](${linkTarget})`;
});

return markdown;
}
13 changes: 13 additions & 0 deletions src/setting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export interface FormatSettings {
UrlLinkFormat: string;
ProperNoun: string;
OrderedListOtherSeparator: string;
isWikiLink2mdRelativePath: boolean;
}

export const DEFAULT_SETTINGS: FormatSettings = {
Expand All @@ -60,6 +61,7 @@ export const DEFAULT_SETTINGS: FormatSettings = {
UrlLinkFormat: "{text}",
ProperNoun: "",
OrderedListOtherSeparator: String.raw``,
isWikiLink2mdRelativePath: true,
};

export class TextFormatSettingTab extends PluginSettingTab {
Expand Down Expand Up @@ -153,6 +155,17 @@ export class TextFormatSettingTab extends PluginSettingTab {


containerEl.createEl("h3", { text: "URL formatting" });
new Setting(containerEl)
.setName("Use relative path when covering wikilinks to plain markdown links.")
.setDesc("Or will use absolute path instead.")
.addToggle((toggle) => {
toggle
.setValue(this.plugin.settings.isWikiLink2mdRelativePath)
.onChange(async (value) => {
this.plugin.settings.isWikiLink2mdRelativePath = value;
await this.plugin.saveSettings();
});
});
new Setting(containerEl)
.setName("The format of result when calling `Remove URL links format in selection`")
.setDesc("Matching with `[{text}]({url})`, use `{text}` if you want to maintain the text, or use `{url}` if you want to maintain the url.")
Expand Down

0 comments on commit 8bf3125

Please sign in to comment.