8e7599150f
This change adds a new command line tool to the Splash family: `SplashMarkdown`. It’s an adapted version of the tool that I’ve been using for months to publish every article on Swift by Sundell, and works by replacing all code blocks within a Markdown file. Adding this will hopefully make Splash even easier to use, without the need for writing custom tooling, or to manually replace each code block within a Markdown file with a “splashed” version.
50 lines
1.6 KiB
Swift
50 lines
1.6 KiB
Swift
/**
|
|
* Splash
|
|
* Copyright (c) John Sundell 2019
|
|
* MIT license - see LICENSE.md
|
|
*/
|
|
|
|
import Foundation
|
|
|
|
/// Type used to decorate a Markdown file with Splash-highlighted code blocks
|
|
public struct MarkdownDecorator {
|
|
private let highlighter = SyntaxHighlighter(format: HTMLOutputFormat())
|
|
private let skipHighlightingPrefix = "no-highlight"
|
|
|
|
public init() {}
|
|
|
|
/// Decorate all code blocks within a given Markdown string. This API assumes
|
|
/// that the passed Markdown is valid. Each code block will be replaced by
|
|
/// Splash-highlighted HTML for that block's code. To skip highlighting for
|
|
/// any given code block, add "no-highlight" next to the opening row of
|
|
/// backticks for that block.
|
|
public func decorate(_ markdown: String) -> String {
|
|
let components = markdown.components(separatedBy: "```")
|
|
var output = ""
|
|
|
|
for (index, component) in components.enumerated() {
|
|
guard index % 2 != 0 else {
|
|
output.append(component)
|
|
continue
|
|
}
|
|
|
|
var code = component.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
|
|
if code.hasPrefix(skipHighlightingPrefix) {
|
|
let charactersToDrop = skipHighlightingPrefix + "\n"
|
|
code = String(code.dropFirst(charactersToDrop.count))
|
|
} else {
|
|
code = highlighter.highlight(code)
|
|
}
|
|
|
|
output.append("""
|
|
<pre class="splash"><code>
|
|
\(code)
|
|
</code></pre>
|
|
""")
|
|
}
|
|
|
|
return output
|
|
}
|
|
}
|