Skip to content

Instantly share code, notes, and snippets.

@ttscoff
Last active October 13, 2023 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ttscoff/86e10e65ddf9f26468c4e0c1612676a2 to your computer and use it in GitHub Desktop.
Save ttscoff/86e10e65ddf9f26468c4e0c1612676a2 to your computer and use it in GitHub Desktop.
Marked Preprocessor for Bear
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'cgi'
input = $stdin.read.force_encoding('utf-8')
# Handle [[links]]
input.gsub!(/\[\[(?<content>.*?)\]\]/) do
m = Regexp.last_match
# Test for link|title matchup
if m['content'] =~ /(?<link>.+)\|(?<title>.+)/
l = Regexp.last_match
title = l['title']
link = l['link']
else
title = m['content']
link = m['content']
end
# Test for note/header matchup
if link =~ %r{(?<page>.+)/(?<header>.+)}
l = Regexp.last_match
link = l['page']
header = l['header']
else
header = nil
end
header = header.nil? ? '' : "&header=#{CGI.escape(header).gsub(/\+/, '%20')}"
url = CGI.escape(link).gsub(/\+/, '%20')
callback = CGI.escape("bear://x-callback-url/create?title=#{url}").gsub(/\+/, '%20')
"[#{title}](bear://x-callback-url/open-note?title=#{url}#{header}&x-error=#{callback})"
end
# Handle ==highlight==
input.gsub!(/==(.*?)==/, '<mark>\1</mark>')
# Handle ~~deletion~~
input.gsub!(/~~(.*?)~~/, '<del>\1</del>')
# Handle ~underline~
input.gsub!(/~(.*?)~/, '<span class="underline">\1</span>')
# Handle tags, linking to tag pages in Bear
input.gsub!(%r{(?!<#)#(?<tag>[^\s#,?.!]+)}i) do
m = Regexp.last_match
tag = CGI.escape(m['tag'])
%(<span class="mkstyledtag"><a href="bear://x-callback-url/open-tag?name=#{tag}">##{m['tag']}</a></span>)
end
puts input
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment