Created
March 13, 2022 14:27
-
-
Save Tokubara/6f7dd45cad648223f214f33f05ccd84e to your computer and use it in GitHub Desktop.
use MarkTex to convert obsidian markdown to latex
This file contains 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
# 去除代码块后面的% | |
$enter_code = 0; | |
while(<>) { | |
if(/\\begin\{langbox\}/) { | |
$enter_code = 1; | |
} elsif(/\\end\{langbox\}/) { | |
$enter_code = 0; | |
} | |
if($enter_code) { | |
s/%(\s*)$/$1/; | |
} | |
print; | |
} |
This file contains 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
use open qw( :std :encoding(UTF-8) ); | |
if (! open fh, "<$ARGV[0]") { | |
die "cannot open "; | |
} | |
@lines = <fh>; | |
# 第一遍扫描, 确定heading_map | |
foreach(@lines) { | |
if(/^(##{1,6})\s/) { # 其实注释也不难处理, 但是一般不用, 就不处理了 | |
$level = (tr/#//); | |
$heading[$level] = 1; | |
} | |
} | |
$cur_level = 1; | |
for ($i = 1; $i <= 6; $i++) { | |
if($heading[$i]==1) { | |
$heading_map[$i]=$cur_level; | |
++$cur_level; | |
} | |
} | |
# 输出行 | |
$notes_prefix="/Users/quebec/notes/vx_attachments/"; | |
foreach(@lines) { | |
if(/^(##{1,6})\s/) { # 其实注释也不难处理, 但是一般不用, 就不处理了 | |
$level = (tr/#//); | |
$new_heading = '#' x $heading_map[$level]; | |
s/^(##{1,6})/$new_heading/; | |
} else { | |
s{!\[\[([^\]]+)\.(png|jpg|jpeg)(\|\d+)?\]\]}{" eq '/' ? "$1" : "$notes_prefix$1") . ".$2)"}eig; | |
} | |
print; | |
} |
This file contains 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
#! /bin/zsh | |
set -e | |
filename=${1##*/} | |
basename=${filename%%.md} | |
mid_md_file=$MHOME/box/markdown2latex/${filename} | |
perl $MHOME/box/markdown2latex/marktex_prepocess.pl $1 > $mid_md_file | |
python /Users/quebec/box/markdown2latex/MarkTex/marktex/cli.py $mid_md_file -o $MHOME/box/markdown2latex/out/ | |
perl -i $MHOME/box/markdown2latex/marktex_postprocess.pl $MHOME/box/markdown2latex/out/${basename}.tex | |
cd $MHOME/box/markdown2latex/out/ | |
latexmk ${basename}.tex | |
cp $MHOME/box/markdown2latex/out/${basename}.pdf ~/Downloads/ | |
open ~/Downloads/${basename}.pdf |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
preprocess does such a thing: if only level 3 and level 5 heading appears, then level 3 becomes level 1, level 5 becomes level 2.
It also convert wikilinks in obsidian to markdown image syntax.
postprocess aims to remove trailing percent signs(
%
).