ghom-eval
Version:
33 lines (32 loc) • 842 B
JavaScript
import prettify from "ghom-prettify";
export const code = {
pattern: /^```(\S+)?\s(.+[^\\])```$/is,
/**
* extract the code from code block and return code
*/
parse(raw) {
const match = this.pattern.exec(raw);
if (!match)
return;
return {
lang: match[1],
content: match[2],
};
},
/**
* inject the code in the code block and return code block
*/
async stringify({ lang, content, format, }) {
return ("```" +
(lang ?? "") +
"\n" +
(format
? await prettify.format(content, format === true ? { lang: lang } : format)
: content) +
"\n```");
},
/**
* format the code using prettier and return it
*/
format: prettify.format,
};