Forked from radekkozak/obsidian_to_marked_wikilink_preprocessor.rb
Created
April 2, 2021 18:00
-
-
Save derekvan/414899b6a1b9947467d42bdbe5057fa6 to your computer and use it in GitHub Desktop.
Obsidian [[WikiLinks|with aliases]] preprocessor for Marked app
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
# Modified for Obsidian WikiLinks by @radekkozak | |
# Original idea for The Archive @mjknght at zettelkasten.de | |
require 'uri' | |
VAULT_NAME = 'ZETTELKASTEN' | |
def class_exists?(class_name) | |
klass = Module.const_get(class_name) | |
return klass.is_a?(Class) | |
rescue NameError | |
return false | |
end | |
if class_exists? 'Encoding' | |
Encoding.default_external = Encoding::UTF_8 if Encoding.respond_to?('default_external') | |
Encoding.default_internal = Encoding::UTF_8 if Encoding.respond_to?('default_internal') | |
end | |
begin | |
input = STDIN.read.force_encoding('utf-8') | |
rescue | |
input = STDIN.read | |
end | |
input.gsub!(/\[\[(.*?)\]\]/) do |m| | |
match = Regexp.last_match | |
if match[1].include?('|') | |
splits = match[1].split('|') | |
"[#{splits[1]}](obsidian://vault/" + ::VAULT_NAME + "/#{URI.escape(splits[0])})" | |
else | |
"[#{match[1]}](obsidian://vault/" + ::VAULT_NAME + "/#{URI.escape(match[1])})" | |
end | |
end | |
print input |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment