Sample code for 30+ languages & platforms
Swift

Markdown to HTML - Full Document, Custom Theme with Code Syntax Highlighting

See more Markdown Examples

Demonstrates how to convert a complete Markdown document to HTML using your own custom theme with Cloudfare Prism's code syntax highlighting.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    success = false

    let options = CkoJsonObject()!

    // Instead of specifying a "theme", provide values for the HTML document parts.

    let sbHead = CkoStringBuilder()!
    var bCrlf: Bool = true
    sbHead.appendLine(str: "<head>", crlf: bCrlf)
    sbHead.appendLine(str: "<style>", crlf: bCrlf)
    sbHead.appendLine(str: "body {", crlf: bCrlf)
    sbHead.appendLine(str: "  font-family: \"Segoe UI\", Tahoma, Geneva, Verdana, sans-serif;", crlf: bCrlf)
    sbHead.appendLine(str: "}", crlf: bCrlf)
    sbHead.appendLine(str: "</style>", crlf: bCrlf)
    sbHead.appendLine(str: "</head>", crlf: bCrlf)

    // Each HTML section can be set individually.
    options.updateString(jsonPath: "docType", value: "<!DOCTYPE html>")
    options.updateString(jsonPath: "rootElement", value: "<html lang=\"en\">")
    options.updateString(jsonPath: "head", value: sbHead.getAsString())
    options.updateString(jsonPath: "bodyStart", value: "<body>\n<div id=\"content\"")
    options.updateString(jsonPath: "bodyEnd", value: "</div>\n</body>")

    // We can add extra content to the bottom of the HTML head and body sections like this:
    // Here we are using the Cloudfare Prism "coy" theme for code syntax higlighting.
    options.updateString(jsonPath: "extraHead", value: "<link rel=\"stylesheet\" href=\"https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism-coy.min.css\" />")
    options.updateString(jsonPath: "extraBody", value: "<script src=\"https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/prism.min.js\"></script><script src =\"https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/plugins/autoloader/prism-autoloader.min.js\"></script>")

    let sbMarkdown = CkoStringBuilder()!
    let sbHtml = CkoStringBuilder()!

    success = sbMarkdown.loadFile(path: "qa_data/markdown/test1.md", charset: "utf-8")
    if success == false {
        print("\(sbMarkdown.lastErrorText!)")
        return
    }

    sbMarkdown.markdown(toHtml: options, sbHtml: sbHtml)
    sbHtml.toCRLF()

    print("\(sbHtml.getAsString()!)")

    // The sample markdown input for this example is identical to the one at Markdown to HTML - Full Document, Raw, where you can view it.

    // Sample HTML output as viewed in a browser:

    // image

}