Generate static sites from Markdown using Swift.
Start by creating a folder for your site.
mkdir MySite
cd MySiteInitialize as an executable Swift Package.
swift package init --type executableEdit your package.swift to first set macOS 11 as the platform and to add SwiftySites as a dependency to both package and target.
let package = Package(
name: "MySite",
platforms: [.macOS(.v11)],
dependencies: [
.package(url: "https://github.com/swiftysites/swiftysites.git", from: "1.0.0-beta.1")
],
…
targets: [
.executableTarget(
name: "MySite",
dependencies: [.product(name: "SwiftySites", package: "swiftysites")]),
…
]
…
)Populate your site with some content.
cat >> Sources/MySite/home.swift << EOF
import Foundation
import SwiftySites
let homePage = Page(
file: URL(fileURLWithPath: "/"),
title: "Home Page",
markdown: """
# Welcome
This is the Home Page.
"""
)
EOFAnd some templates.
cat >> Sources/MySite/homeTemplate.swift << EOF
import SwiftySites
let homeTemplate = BasicSite.templateA(match: #"/"#, suffix: "html") { site, page in
"""
<html>
<head><title>\(page.title)</title></head>
<body>
<div>
\(page.content)
</div>
</body>
</html>
"""
}
EOFTo tie it all together, let's define the site itself in main.swift.
rm Sources/MySite/main.swift
cat >> Sources/MySite/main.swift << EOF
import SwiftySites
BasicSite(
SiteConfig(title: "My Site"),
contentA: [homePage],
templates: [homeTemplate]
).render()
EOFTip: Check out the Samples folder for more examples on how to write and organize your content files.
Finally build and run your executable to generate your static site!
swift runAll the generated files will be in the www folder.
Spin up a web server to publish your static site locally.
python -m http.server --directory wwwDirect your browser to http://localhost:8000/index.html to see your site.
Use the following command:
swift package --allow-writing-to-directory [output-directory-path] \
generate-documentation --target [target-name] --disable-indexing \
--output-path [output-directory-path] \
--transform-for-static-hosting \
--hosting-base-path [hosting-base-path]