Skip to content

Commit 12b2d50

Browse files
author
Jesse Haigh
committed
Add option to copy a code block parsed from the language string, delimited by commas
1 parent 753c0dc commit 12b2d50

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

Sources/SwiftDocC/Model/Rendering/RenderContentCompiler.swift

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,34 @@ struct RenderContentCompiler: MarkupVisitor {
4747

4848
mutating func visitCodeBlock(_ codeBlock: CodeBlock) -> [any RenderContent] {
4949
// Default to the bundle's code listing syntax if one is not explicitly declared in the code block.
50-
return [RenderBlockContent.codeListing(.init(syntax: codeBlock.language ?? bundle.info.defaultCodeListingLanguage, code: codeBlock.code.splitByNewlines, metadata: nil, copyToClipboard: false))]
50+
struct ParsedOptions {
51+
var lang: String?
52+
var copy = false
53+
}
54+
55+
func parseLanguageString(_ input: String?) -> ParsedOptions {
56+
guard let input else { return ParsedOptions() }
57+
58+
let parts = input
59+
.split(separator: ",")
60+
.map { $0.trimmingCharacters(in: .whitespaces) }
61+
62+
var options = ParsedOptions()
63+
64+
for part in parts {
65+
let lower = part.lowercased()
66+
if lower == "copy" {
67+
options.copy = true
68+
} else if options.lang == nil {
69+
options.lang = part
70+
}
71+
}
72+
return options
73+
}
74+
75+
let options = parseLanguageString(codeBlock.language)
76+
77+
return [RenderBlockContent.codeListing(.init(syntax: options.lang ?? bundle.info.defaultCodeListingLanguage, code: codeBlock.code.splitByNewlines, metadata: nil, copyToClipboard: options.copy))]
5178
}
5279

5380
mutating func visitHeading(_ heading: Heading) -> [any RenderContent] {

0 commit comments

Comments
 (0)