KituraMarkdown

public class KituraMarkdown : TemplateEngine

A Kitura TemplateEngine that enables a Kitura server to render HTML content generated from Markdown templates (.md files).

This class also provides helper methods for converting Markdown formatted text from a String or Data to HTML.

Note

Under the covers this templating engine uses the cmark C language reference implementation of Markdown.

Usage Example:

router.add(templateEngine: KituraMarkdown())

router.get("/docs") { _, response, next in
    try response.render("Example.md", context: [String:Any]())
    response.status(.OK)
    next()
}
  • The file extension of files that will be rendered by the KituraMarkdown template engine. By default, Kitura will search for these in the ./Views/ directory, which can be customized by setting the router.viewsPath property.

    Declaration

    Swift

    public let fileExtension: String
  • Create a KituraMarkdown instance that can be registered with a Kitura router.

    Declaration

    Swift

    public init()
  • Take a template file in Markdown format and generate HTML format content to be sent back to the client.

    Throws

    An error if the template file cannot be read.

    Declaration

    Swift

    public func render(filePath: String, context: [String : Any]) throws -> String

    Parameters

    filePath

    The path of the template file in Markdown format to use when generating the content.

    context

    A set of variables in the form of a Dictionary of Key/Value pairs. Note: This parameter is ignored at this time.

    Return Value

    A String containing an HTML representation of the text marked up using Markdown.

  • Take a template file in Markdown format and generate HTML format content to be sent back to the client.

    Note that rendering of context is not supported at this time, and the with and forKey parameters are currently ignored.

    Throws

    An error if the template file cannot be read.

    Declaration

    Swift

    public func render<T>(filePath: String, with: T, forKey: String?, options: RenderingOptions, templateName: String) throws -> String where T : Encodable

    Parameters

    filePath

    The path of the template file in Markdown format to use when generating the content.

    with

    A value that conforms to Encodable. Note: This parameter is ignored at this time.

    forKey

    A value used to match the Encodable values to the correct variable in a template file. Note: This parameter is ignored at this time.

    options

    A RenderingOptions used to customize the output. The HTML page template can be customized by providing an instance of MarkdownOptions.

    Return Value

    A String containing an HTML representation of the text marked up using Markdown.

  • Take a template file in Markdown format and generate HTML format content to be sent back to the client.

    Note that rendering of context is not supported at this time, and the context parameter is currently ignored.

    Throws

    An error if the template file cannot be read.

    Declaration

    Swift

    public func render(filePath: String, context: [String: Any],
                       options: RenderingOptions) throws -> String

    Parameters

    filePath

    The path of the template file in Markdown format to use when generating the content.

    context

    A set of variables in the form of a Dictionary of Key/Value pairs. Note: This parameter is ignored at this time

    options

    A RenderingOptions used to customize the output. The HTML page template can be customized by providing an instance of MarkdownOptions.

    Return Value

    A String containing an HTML representation of the text marked up using Markdown.

  • Generate HTML content from a Data struct containing text marked up in Markdown in the form of UTF-8 bytes.

    Declaration

    Swift

    public static func render(from: Data) -> String

    Parameters

    from

    The Data struct containing markdown text in UTF-8.

    Return Value

    A String containing an HTML representation of the text marked up using Markdown.

  • Generate HTML content from a String containing text marked up in Markdown.

    Declaration

    Swift

    public static func render(from: String) -> String

    Parameters

    from

    The String containing markdown text in UTF-8.

    Return Value

    A String containing an HTML representation of the text marked up using Markdown.

  • Generate an HTML page from a String containing text marked up in Markdown.

    Declaration

    Swift

    public static func render(from: String, pageTemplate: String) -> String

    Parameters

    from

    The String containing markdown in UTF-8.

    pageTemplate

    The HTML page template to use. The page template should contain <snippetInsertLocation></snippetInsertLocation>, which will be substituted with the generated HTML content. Note: If you do not wish to customize the page template, "default" can be specified.

    Return Value

    A String containing an HTML representation of the text marked up using Markdown, enclosed in an HTML page corresponding to the pageTemplate supplied.