docdef/source/outputs/implementations/markdown.ts

249 lines
4.8 KiB
TypeScript
Raw Normal View History

2024-11-17 15:02:51 +01:00
/**
*/
class type_output_markdown implements type_output<string>
{
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<string>(
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,
}
),
2026-02-23 20:16:38 +01:00
"image": ({"url": url, "label": label}) => lib_plankton.string.coin(
"![{{label}}]({{url}})",
{
"url": url,
"label": label,
}
),
"paragraph": ({"content": content}) => lib_plankton.string.coin(
"{{content}}\n\n",
{
"content": this.render_object_internal(
content,
{
"depth": (options.depth + 1),
"headline_level": options.headline_level,
"list_level": options.list_level,
}
),
}
),
2024-11-17 15:02:51 +01:00
"section": ({"title": title, "content": content}) => lib_plankton.string.coin(
"{{head}}\n\n{{body}}\n\n\n",
{
"head": lib_plankton.string.coin(
"{{prefix}} {{value}}",
{
2026-02-23 20:16:38 +01:00
"prefix": "#".repeat(options.headline_level + 1),
2024-11-17 15:02:51 +01:00
"value": title,
}
),
"body": this.render_object_internal(
content,
{
"depth": (options.depth + 1),
2026-02-23 20:16:38 +01:00
"headline_level": (options.headline_level + 1),
2024-11-17 15:02:51 +01:00
"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,
2026-02-23 20:16:38 +01:00
"headline_level": options.headline_level,
2024-11-17 15:02:51 +01:00
"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),
2026-02-23 20:16:38 +01:00
"headline_level": options.headline_level,
2024-11-17 15:02:51 +01:00
"list_level": (options.list_level + 1),
}
),
},
)
)
.join(
"\n"
)
),
}
),
"table": ({"head": head, "rows": rows}) => lib_plankton.string.coin(
2026-02-23 20:16:38 +01:00
"{{head}}\n{{separation}}\n{{body}}\n\n",
2024-11-17 15:02:51 +01:00
{
2026-02-23 20:16:38 +01:00
"head": lib_plankton.string.coin(
"| {{entries}} |",
{
"entries": (
head
.map(
(object) => this.render_object_internal(
object,
{
"depth": (options.depth + 1),
"headline_level": options.headline_level,
"list_level": options.list_level,
}
),
)
.join(" | ")
),
}
),
"separation": lib_plankton.string.coin(
"| {{entries}} |",
{
"entries": (
head
.map((object) => "-- ")
.join(" | ")
),
}
),
"body": (
rows
.map(
row => lib_plankton.string.coin(
"| {{entries}} |",
{
"entries": (
row
.map(
(object) => this.render_object_internal(
object,
{
"depth": (options.depth + 1),
"headline_level": options.headline_level,
"list_level": options.list_level,
}
),
)
.join(" | ")
),
}
)
)
.join("\n")
),
2024-11-17 15:02:51 +01:00
}
),
},
{
"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(
{
}
)
)
);