Last active
November 26, 2022 19:40
-
-
Save joakim/e8e46249125e35514f20ed2a3e43a969 to your computer and use it in GitHub Desktop.
kesh version of Lagrange MIME hook for formatting of inline emphasis
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
import [ read-lines ]: 'https://deno.land/std/io/buffer.ts' | |
import [ copy ]: 'https://deno.land/std/streams/conversion.ts' | |
#type: (search: #RegExp, replace: #text) | |
-- Pass-through the header | |
print "20 { Deno.args.join(';') }\r" | |
-- Check for type=inline parameter | |
inline: Deno.args.find (arg) -> | |
arg.trim().starts-with('type=') and arg.split('=').1.split(',').includes('inline') | |
-- Pass-through the body if no type=inline | |
if not inline | |
await copy(Deno.stdin, Deno.stdout) | |
Deno.exit() | |
types: #type[]: [ | |
(re`(?g)_([^_]+)_`, "\x1b[3m$1\x1b[0m") -- italic | |
(re`(?g)\*\*([^*]+)\*\*`, "\x1b[1m$1\x1b[0m") -- bold | |
] | |
replace: (text: #text, type: #type) -> #text | |
text.replace-all(type...) | |
format: (text: #text) -> #text | |
types.reduce(replace, text) | |
let preformatting: false | |
loop await read-lines(Deno.stdin) as line | |
-- Toggle preformatting lines | |
if line.starts-with('```') | |
set preformatting: not preformatting | |
print line | |
-- Skip preformatted, header and link lines | |
else if preformatting or line.starts-with('#') or line.starts-with('=>') | |
print line | |
-- Format list item lines | |
else if line.starts-with('* ') | |
print "* { format line.slice(2) }" | |
-- Format text and quote lines | |
else | |
print format line |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Original: lagrange-inline-formatting.ts