|
| 1 | +# SagaUtils |
| 2 | + |
| 3 | +A collection of utilities for [Saga](https://github.com/loopwerk/Saga): HTML transformations and useful String extensions. |
| 4 | + |
| 5 | +## Usage |
| 6 | +Include `SagaUtils` in your Package.swift: |
| 7 | + |
| 8 | +```swift |
| 9 | +let package = Package( |
| 10 | + dependencies: [ |
| 11 | + .package(url: "https://github.com/loopwerk/Saga", from: "2.0.3"), |
| 12 | + .package(url: "https://github.com/loopwerk/SagaUtils", from: "0.1.0"), |
| 13 | + ], |
| 14 | + targets: [ |
| 15 | + .target( |
| 16 | + name: "MyWebsite", |
| 17 | + dependencies: ["Saga", "SagaUtils"]), |
| 18 | + ] |
| 19 | +) |
| 20 | +``` |
| 21 | + |
| 22 | +## HTML Transformations |
| 23 | + |
| 24 | +SagaUtils provides composable HTML transformations powered by [SwiftSoup](https://github.com/scinfu/SwiftSoup). Each transformation operates on a SwiftSoup `Document` and can be combined using `swiftSoupProcessor`: |
| 25 | + |
| 26 | +```swift |
| 27 | +import SagaUtils |
| 28 | + |
| 29 | +try await Saga(input: "content", output: "deploy") |
| 30 | + .register( |
| 31 | + folder: "articles", |
| 32 | + metadata: ArticleMetadata.self, |
| 33 | + readers: [.parsleyMarkdownReader], |
| 34 | + itemProcessor: swiftSoupProcessor(generateTOC, convertAsides, processExternalLinks, addCodeBlockTitles), |
| 35 | + writers: [.itemWriter(swim(renderArticle))] |
| 36 | + ) |
| 37 | + .run() |
| 38 | +``` |
| 39 | + |
| 40 | +### Available Transformations |
| 41 | + |
| 42 | +- **`addHeadingAnchors`** — Adds `<a name="slug"></a>` anchors to h1, h2, h3 headings. |
| 43 | +- **`generateTOC`** — Replaces a `%TOC%` placeholder with a `<nav class="toc">` generated from headings. Also adds heading anchors, so there's no need to also use `addHeadingAnchors`. Use `generateTOC(placeholder: "@TOC")` for a custom placeholder. |
| 44 | +- **`convertAsides`** — Converts blockquotes with `[!TYPE]` syntax to `<aside class="type">` elements. For example, `[!WARNING]` becomes `<aside class="warning">`. |
| 45 | +- **`processExternalLinks`** — Adds `target="_blank"` and `rel="nofollow"` to external links. |
| 46 | + |
| 47 | +You can also write your own transformations with the signature `(Document) throws -> Void` and pass them to `swiftSoupProcessor`. |
| 48 | + |
| 49 | +## String Extensions |
| 50 | + |
| 51 | +Useful extensions on `String`: |
| 52 | + |
| 53 | +```swift |
| 54 | +// Strip HTML tags, keeping code block content |
| 55 | +"<p>Hello <strong>world</strong></p>".plainText // "Hello world" |
| 56 | + |
| 57 | +// Strip HTML tags and code blocks (useful for word counting) |
| 58 | +body.textOnly |
| 59 | + |
| 60 | +// Count words |
| 61 | +body.textOnly.wordCount |
| 62 | + |
| 63 | +// Truncate with word boundary awareness (inspired by Jinja2) |
| 64 | +text.truncate(length: 200) |
| 65 | +text.truncate(length: 200, killWords: true, end: "…") |
| 66 | +``` |
0 commit comments