From a605e5713d7582bf61934b0de67610d3fe3b60ec Mon Sep 17 00:00:00 2001 From: Fenris Wolf Date: Sun, 17 Nov 2024 15:02:51 +0100 Subject: [PATCH] [add] output:markdown --- source/outputs/implementations/markdown.ts | 174 +++++++++++++++++++++ tools/makefile | 1 + 2 files changed, 175 insertions(+) create mode 100644 source/outputs/implementations/markdown.ts diff --git a/source/outputs/implementations/markdown.ts b/source/outputs/implementations/markdown.ts new file mode 100644 index 0000000..72a4aac --- /dev/null +++ b/source/outputs/implementations/markdown.ts @@ -0,0 +1,174 @@ + +/** + */ +class type_output_markdown implements type_output +{ + public readonly kind : string = "markdown"; + public readonly data : {}; + public constructor(data : {}) {this.data = data;} + + /** + */ + private render_object_internal( + object : type_object, + options : { + depth ?: int; + headline_level ?: int; + list_level ?: int; + } = {} + ) : string + { + options = Object.assign( + { + "depth": 0, + "headline_level": 0, + "list_level": 0, + }, + options + ); + return lib_plankton.call.distinguish( + object, + { + "text": ({"content": content}) => lib_plankton.string.coin( + "{{content}}", + { + "content": content, + } + ), + "code": ({"content": content}) => lib_plankton.string.coin( + "`{{content}}`", + { + "content": content, + } + ), + "link": ({"target": target, "label": label}) => lib_plankton.string.coin( + "[{{label}}]({{target}})", + { + "target": target, + "label": label, + } + ), + "section": ({"title": title, "content": content}) => lib_plankton.string.coin( + "{{head}}\n\n{{body}}\n\n\n", + { + "head": lib_plankton.string.coin( + "{{prefix}} {{value}}", + { + "prefix": "#".repeat(options.level + 1), + "value": title, + } + ), + "body": this.render_object_internal( + content, + { + "depth": (options.depth + 1), + "headline_level": (options.level + 1), + "list_level": options.list_level, + } + ), + } + ), + "group": ({"members": members}) => lib_plankton.string.coin( + "{{members}}", + { + "members": ( + members + .map( + x => this.render_object_internal( + x, + { + "depth": options.depth, + "headline_level": options.level, + "list_level": options.list_level + } + ) + ) + .join( + "" + ) + ) + } + ), + "list": ({"items": items}) => lib_plankton.string.coin( + "{{items}}", + { + "items": ( + items + .map( + item => lib_plankton.string.coin( + "{{indentation}}- {{content}}", + { + "indentation": " ".repeat(options.list_level), + "content": this.render_object_internal( + item, + { + "depth": (options.depth + 1), + "headline_level": options.level, + "list_level": (options.list_level + 1), + } + ), + }, + ) + ) + .join( + "\n" + ) + ), + } + ), + "table": ({"head": head, "rows": rows}) => lib_plankton.string.coin( + "(unhandled)", + { + } + ), + }, + { + "fallback": (object) => lib_plankton.string.coin( + "(unhandled)", + { + } + ), + } + ); + } + + + /** + * @implementation + */ + public render_object( + object : type_object + ) : string + { + return this.render_object_internal(object); + } + + + /** + * @implementation + */ + public render_document( + document : type_document + ) : string + { + return this.render_object_internal( + document.content_main, + { + } + ); + } + +} + + +/** + */ +output_kind_register( + "markdown", + (data, sub) => ( + new type_output_markdown( + { + } + ) + ) +); diff --git a/tools/makefile b/tools/makefile index 3aa4fce..917b6b1 100644 --- a/tools/makefile +++ b/tools/makefile @@ -35,6 +35,7 @@ ${dir_temp}/sd-unlinked.js: \ ${dir_source}/document.ts \ ${dir_source}/outputs/base.ts \ ${dir_source}/outputs/implementations/json.ts \ + ${dir_source}/outputs/implementations/markdown.ts \ ${dir_source}/outputs/implementations/html.ts \ ${dir_source}/outputs/implementations/tex.ts \ ${dir_source}/main.ts