Skip to content

Commit 31d46a1

Browse files
committed
enable magic aside blocks in snippet captions
1 parent 292ea8b commit 31d46a1

File tree

4 files changed

+82
-29
lines changed

4 files changed

+82
-29
lines changed

Sources/MarkdownPluginSwift/Markdown.SwiftLanguage.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ extension Markdown.SwiftLanguage:Markdown.CodeLanguageType
2626
extension Markdown.SwiftLanguage
2727
{
2828
public
29-
func parse(snippet utf8:[UInt8]) -> (overview:String, slices:[Markdown.SnippetSlice])
29+
func parse(snippet utf8:[UInt8]) -> (caption:String, slices:[Markdown.SnippetSlice])
3030
{
3131
// It is safe to escape the pointer to ``Parser.parse(source:maximumNestingLevel:)``,
3232
// see: https://swiftinit.org/docs/swift-syntax/swiftparser/parser.init(_:maximumnestinglevel:parsetransition:arena:)
@@ -36,7 +36,7 @@ extension Markdown.SwiftLanguage
3636
}
3737

3838
var start:AbsolutePosition = parsed.position
39-
var text:String = ""
39+
var caption:String = ""
4040
lines:
4141
for piece:TriviaPiece in parsed.leadingTrivia
4242
{
@@ -75,8 +75,8 @@ extension Markdown.SwiftLanguage
7575
fatalError("Encountered a line comment with no leading slashes!")
7676
}
7777

78-
text += line[i...].drop(while: \.isWhitespace)
79-
text.append("\n")
78+
caption += line[i...].drop(while: \.isWhitespace)
79+
caption.append("\n")
8080
}
8181

8282
var parser:SnippetParser = .init(sourcemap: .init(fileName: "", tree: parsed),
@@ -138,6 +138,6 @@ extension Markdown.SwiftLanguage
138138
return .init(id: slice.id, line: slice.line, code: bytecode)
139139
}
140140

141-
return (text, rendered)
141+
return (caption, rendered)
142142
}
143143
}

Sources/MarkdownSemantics/Markdown.BlockInterpreter.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,8 @@ extension Markdown.BlockInterpreter
274274
var items:[Markdown.BlockItem] = []
275275
for item:Markdown.BlockItem in list.elements
276276
{
277-
guard let prefix:Markdown.BlockPrefix = .extract(from: &item.elements)
277+
guard
278+
let prefix:Markdown.BlockPrefix = .extract(from: &item.elements)
278279
else
279280
{
280281
items.append(item)

Sources/MarkdownSemantics/Snippets/Markdown.Snippet.swift

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ extension Markdown
1616
public
1717
let slices:OrderedDictionary<String, SnippetSlice>
1818

19-
@inlinable public
19+
private
2020
init(id:Int32,
2121
caption:[Markdown.BlockElement],
2222
slices:OrderedDictionary<String, SnippetSlice>)
@@ -27,3 +27,72 @@ extension Markdown
2727
}
2828
}
2929
}
30+
extension Markdown.Snippet
31+
{
32+
public
33+
init(id:Int32,
34+
caption:String,
35+
slices:[Markdown.SnippetSlice],
36+
using parser:borrowing some MarkdownParser)
37+
{
38+
let index:OrderedDictionary<String, Markdown.SnippetSlice> = slices.reduce(
39+
into: [:])
40+
{
41+
$0[$1.id] = $1
42+
}
43+
44+
if caption.allSatisfy(\.isWhitespace)
45+
{
46+
self.init(id: id, caption: [], slices: index)
47+
return
48+
}
49+
50+
// Most documentation magic is not available to snippet captions (recursive snippets
51+
// especially), but we still want the magic aside blocks to work.
52+
var blocks:[Markdown.BlockElement] = []
53+
for block:Markdown.BlockElement in parser.parse(.init(location: .init(
54+
position: .zero,
55+
file: id),
56+
text: caption))
57+
{
58+
switch block
59+
{
60+
case let list as Markdown.BlockListUnordered:
61+
var items:[Markdown.BlockItem] = []
62+
for item:Markdown.BlockItem in list.elements
63+
{
64+
if let prefix:Markdown.BlockPrefix = .extract(from: &item.elements),
65+
case .keywords(let aside) = prefix
66+
{
67+
blocks.append(aside(item.elements))
68+
}
69+
else
70+
{
71+
items.append(item)
72+
}
73+
}
74+
if !items.isEmpty
75+
{
76+
list.elements = items
77+
blocks.append(list)
78+
}
79+
80+
case let quote as Markdown.BlockQuote:
81+
if case .keywords(let aside) = Markdown.BlockPrefix.extract(
82+
from: &quote.elements)
83+
{
84+
blocks.append(aside(quote.elements))
85+
}
86+
else
87+
{
88+
blocks.append(quote)
89+
}
90+
91+
case let block:
92+
blocks.append(block)
93+
}
94+
}
95+
96+
self.init(id: id, caption: blocks, slices: index)
97+
}
98+
}

Sources/SymbolGraphLinker/StaticLinker.swift

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -278,30 +278,13 @@ extension StaticLinker
278278
// snippets from other snippets.
279279
return supplements.reduce(into: [:])
280280
{
281-
let (caption, slices):(String, [Markdown.SnippetSlice]) = swift.parse(
281+
let snippet:(caption:String, slices:[Markdown.SnippetSlice]) = swift.parse(
282282
snippet: $1.utf8)
283283

284-
let id:Int32 = self.symbolizer.intern($1.path)
285-
let blocks:[Markdown.BlockElement]
286-
287-
if caption.allSatisfy(\.isWhitespace)
288-
{
289-
blocks = []
290-
}
291-
else
292-
{
293-
blocks = self.doccommentParser.parse(.init(
294-
location: .init(position: .zero, file: id),
295-
text: caption))
296-
}
297-
298-
let index:OrderedDictionary<String, Markdown.SnippetSlice> = slices.reduce(
299-
into: [:])
300-
{
301-
$0[$1.id] = $1
302-
}
303-
304-
$0[$1.name] = .init(id: id, caption: blocks, slices: index)
284+
$0[$1.name] = .init(id: self.symbolizer.intern($1.path),
285+
caption: snippet.caption,
286+
slices: snippet.slices,
287+
using: self.doccommentParser)
305288
}
306289
}
307290

0 commit comments

Comments
 (0)