Can I use a html template file to generate html from markdown? #553

Closed
opened 2026-01-29 14:39:34 +00:00 by claunia · 4 comments
Owner

Originally created by @kunlinyu on GitHub (Aug 25, 2022).

I use a string "header" and a string "footer", and connect them (header + html + footer) to get the full html.
Can I use a html template file to achieve this?

    public static void GenerateHtmlFromMarkdown()
    {
        string header = @"<!DOCTYPE html>
<html>
<head>
  <meta http-equiv=""Content-Type"" content=""text/html; charset=utf-8"">
  <title>ReadMe</title>
</head>
<body>
";
        string footer = "</body>\n</html>\n";

        string markdownPath = "README.md";
        string indexPath = "./index.html";

        string markdown = File.ReadAllText(markdownPath);

        MarkdownPipeline pipeline = new MarkdownPipelineBuilder().UseAdvancedExtensions().Build();
        string html = Markdown.ToHtml(markdown, pipeline);
        string fullHtml = header + html + footer;

        File.WriteAllText(indexPath, fullHtml);
    }
Originally created by @kunlinyu on GitHub (Aug 25, 2022). I use a string "header" and a string "footer", and connect them (header + html + footer) to get the full html. Can I use a html template file to achieve this? ```C# public static void GenerateHtmlFromMarkdown() { string header = @"<!DOCTYPE html> <html> <head> <meta http-equiv=""Content-Type"" content=""text/html; charset=utf-8""> <title>ReadMe</title> </head> <body> "; string footer = "</body>\n</html>\n"; string markdownPath = "README.md"; string indexPath = "./index.html"; string markdown = File.ReadAllText(markdownPath); MarkdownPipeline pipeline = new MarkdownPipelineBuilder().UseAdvancedExtensions().Build(); string html = Markdown.ToHtml(markdown, pipeline); string fullHtml = header + html + footer; File.WriteAllText(indexPath, fullHtml); } ```
claunia added the question label 2026-01-29 14:39:34 +00:00
Author
Owner

@xoofx commented on GitHub (Aug 25, 2022):

Can I use a html template file to achieve this?

I'm not sure to understand. Do you ask if markdig provides a templating engine or are you asking if your code is correctly using markdig?

@xoofx commented on GitHub (Aug 25, 2022): > Can I use a html template file to achieve this? I'm not sure to understand. Do you ask if markdig provides a templating engine or are you asking if your code is correctly using markdig?
Author
Owner

@kunlinyu commented on GitHub (Aug 25, 2022):

@xoofx
What I want is, using a html template file "template.html.in":

<html>
<head>
  <meta http-equiv=""Content-Type"" content=""text/html; charset=utf-8"">
  <title>ReadMe</title>
</head>
<body>
@MARKDOWN@
</body>
</html>

to generate the whole html file

    public static void GenerateHtmlFromMarkdown()
    {
        string markdownPath = "README.md";
        string indexPath = "./index.html";
        string templatePath = "./template.html.in";

        string markdown = File.ReadAllText(markdownPath);

        MarkdownPipeline pipeline = new MarkdownPipelineBuilder().UseAdvancedExtensions().Build();
        string fullHtml = Markdown.ToHtmlWithTemplate(markdown, pipeline, File.ReadAllText(templatePath));

        File.WriteAllText(indexPath, fullHtml);
    }

My question is, does MarkDig has such kind of feature?

@kunlinyu commented on GitHub (Aug 25, 2022): @xoofx What I want is, using a html template file "template.html.in": ```html <html> <head> <meta http-equiv=""Content-Type"" content=""text/html; charset=utf-8""> <title>ReadMe</title> </head> <body> @MARKDOWN@ </body> </html> ``` to generate the whole html file ```C# public static void GenerateHtmlFromMarkdown() { string markdownPath = "README.md"; string indexPath = "./index.html"; string templatePath = "./template.html.in"; string markdown = File.ReadAllText(markdownPath); MarkdownPipeline pipeline = new MarkdownPipelineBuilder().UseAdvancedExtensions().Build(); string fullHtml = Markdown.ToHtmlWithTemplate(markdown, pipeline, File.ReadAllText(templatePath)); File.WriteAllText(indexPath, fullHtml); } ``` My question is, does MarkDig has such kind of feature?
Author
Owner

@xoofx commented on GitHub (Aug 25, 2022):

My question is, does MarkDig has such kind of feature?

There is no such feature and no plan to implement this. In your example above, replacing @MARKDOWN@ is one string.Replace away. Templating should be done at different level, usually with a templating engine (like Scriban) as it often requires conditionals/loops and variables to be anything useful.

@xoofx commented on GitHub (Aug 25, 2022): > My question is, does MarkDig has such kind of feature? There is no such feature and no plan to implement this. In your example above, replacing `@MARKDOWN@` is one `string.Replace` away. Templating should be done at different level, usually with a templating engine (like [Scriban](https://github.com/scriban/scriban)) as it often requires conditionals/loops and variables to be anything useful.
Author
Owner

@kunlinyu commented on GitHub (Aug 25, 2022):

@xoofx
Thanks. Understand.

@kunlinyu commented on GitHub (Aug 25, 2022): @xoofx Thanks. Understand.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/markdig#553