Skip to content

swiftysites/swiftysites

 
 

Repository files navigation

SwiftySites

Generate static sites from Markdown using Swift.

Getting Started

Start by creating a folder for your site.

mkdir MySite
cd MySite

Initialize as an executable Swift Package.

swift package init --type executable

Edit 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.
    
    """
)
EOF

And 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>
    """
}
EOF

To 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()
EOF

Tip: 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 run

All 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 www

Direct your browser to http://localhost:8000/index.html to see your site.

Contributing

Generate DocC

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]

About

Generate static sites from Markdown using Swift

Resources

License

Stars

Watchers

Forks

Languages

  • Swift 99.3%
  • Shell 0.7%