一个使用 Markdown 模板的 Kitura 模板引擎。
Kitura-Markdown 使得 Kitura 服务器能够提供由 Markdown 模板 (.md 文件) 生成的 HTML 内容。
Markdown 是一种轻量级的标记语言,使用纯文本格式语法。 掌握 Markdown 提供了关于如何编写 Markdown 文件的文档和示例。 默认情况下,Kitura 路由器将在 Views 文件夹中查找扩展名为 .md 的 Markdown 文件。
将 Kitura-Markdown 包添加到应用程序的 Package.swift 文件中的依赖项中。 将 "x.x.x" 替换为最新的 Kitura-Markdown 版本。
.package(url: "https://github.com/Kitura/Kitura-Markdown.git", from: "x.x.x")
将 KituraMarkdown 添加到您的目标的依赖项中
.target(name: "example", dependencies: ["KituraMarkdown"]),
import KituraMarkdown
以下示例使用 kitura init 生成的服务器,并对其进行修改以提供来自 .md 文件的 Markdown 格式文本。
在此示例中将要编辑的文件如下
<ServerRepositoryName>
├── Package.swift
├── Sources
│ └── Application
│ └── Application.swift
└── Views
└── Example.md
Views 文件夹和 Example.md 文件将在稍后创建,因为它们没有被 kitura init 初始化。
将依赖项添加到您的 Package.swift 文件(如上文添加依赖中所定义)。
在 Application.swift 文件中,添加以下代码以在 "/docs" 路由上渲染 Example.md 模板文件。
import KituraMarkdown
在 postInit() 函数中添加以下代码
router.add(templateEngine: KituraMarkdown())
router.get("/docs") { _, response, next in
try response.render("Example.md", context: [String:Any]())
response.status(.OK)
next()
}
创建 Views 文件夹,并将以下 Markdown 模板代码放入名为 Example.md 的文件中
It's very easy to make some words **bold** and other words *italic* with Markdown. You can even [link to Kitura](https://github.com/Kitura/Kitura) and write code examples:
`print("Hello world!")`
当服务器运行时,访问 https://:8080/docs 以查看渲染的 Markdown 模板。
有关更多信息,请访问我们的 API 参考。
我们喜欢讨论服务器端的 Swift 和 Kitura。 加入我们的 Slack 来认识团队!
此库在 Apache 2.0 许可下发布。 完整的许可文本可在 LICENSE 中找到。