feat: extract markdown preview

This commit is contained in:
2024-08-06 12:26:38 +03:00
parent 2e5d2edc53
commit fe33bf01cc
3 changed files with 92 additions and 56 deletions

View File

@@ -367,7 +367,7 @@ autoload _docker-exec
autoload _docker-volume-path
autoload _prj
autoload _src
autoload _srcp
autoload _psrc
# reload entire shell
reload-zsh() {
@@ -600,61 +600,6 @@ uridecode() {
posix_compliant "${*}"
}
# convert markdown to html and output to stdout
md2html() {
file=${1:-README.md}
pandoc $file
}
# convert markdown to html and open in browser
mdp() {
file=${1:-README.md}
html_prefix="
<html>
<head>
<title>$file</title>
<style>
:root {
--font-normal: Helvetica, Arial, sans-serif;
--font-mono: 'MesloLGS Nerd Font Mono', monospace;
}
html, body {
font-family: var(--font-normal);
}
body {
margin:40px auto 0;
max-width:800px;
font-size:16;
}
code, pre {
background-color:#f4f4f4;
padding:5px;
border-radius:5px;
font-family: var(--font-mono);
}
code {
display: inline-block;
}
pre > code {
background-color:unset;
font-family: var(--font-mono);
display: block;
}
</style>
</head>
<body>
"
echo "Opening HTML preview for $(basename $file)..."
filewoext="$(basename ${file%.*})"
f="$(mktemp)-$filewoext.html"
echo $html_prefix>$f
md2html $file >>$f
echo "</body></html>" >>$f
open -u "file:///$f"
# echo "Opening file:///$f"
($SHELL -c "sleep 10; rm $f; exit 0" &)
}
# sets pnpm version on closest package.json to current version
set-pnpm-pkg-version() {
fl=$(find-up package.json)

View File

@@ -0,0 +1,41 @@
#!/usr/bin/env zsh
# convert markdown to html and output to stdout
md2html() {
file=${1:-README.md}
pandoc $file
}
# convert markdown to html and open in browser
mdp() {
filename=${1:-README.md}
html_file="$DOTFILES/plugins/mdp-template.html"
title=$(basename $filename)
filewoext="$(basename ${filename%.*})"
echo "Opening HTML preview for $title..."
f="$(mktemp -d)/$filewoext.html"
bodyf="$f.part"
# copy template
cat $html_file>$f
# replace title
sed -E "s/<!--TITLE-->%/$title/" $f > $f.tmp
mv $f.tmp $f
# generate md preview
md2html $filename >$bodyf
# replace body
sed -E "/<!--BODY-->/r $bodyf" $f > $f.tmp
mv $f.tmp $f
rm $bodyf
# open the file in browser
open -u "file:///$f"
# remove temp files
($SHELL -c "sleep 10; rm $f; exit 0" &)
}

50
plugins/mdp-template.html Normal file
View File

@@ -0,0 +1,50 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/5.6.1/github-markdown.min.css"
integrity="sha512-heNHQxAqmr9b5CZSB7/7NSC96qtb9HCeOKomgLFv9VLkH+B3xLgUtP73i1rM8Gpmfaxb7262LFLJaH/hKXyxyA=="
crossorigin="anonymous"
referrerpolicy="no-referrer"
/>
<title><!--TITLE--></title>
<style>
:root {
--bgColor-default: #ffffff;
}
.markdown-body {
box-sizing: border-box;
min-width: 200px;
max-width: 980px;
margin: 0 auto;
padding: 45px;
}
@media (max-width: 767px) {
.markdown-body {
padding: 15px;
}
}
body {
margin: 40px auto 0;
font-size: 16px;
background-color: var(--bgColor-default);
}
@media (prefers-color-scheme: dark) {
:root {
--bgColor-default: #0d1117;
}
}
</style>
</head>
<body>
<div class="markdown-body">
<!--BODY-->
</div>
</body>
</html>