Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions Sources/StableBar/Popups/MarkdownBlocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ enum MarkdownBlock: Equatable {
case code(String)
case table(header: [String], rows: [[String]])

/// The blocks of `markdown`. A code fence that isn't closed yet, as while an answer arrives, runs to the end, and a
/// table shows as text until its second line marks it as one.
static func blocks(_ markdown: String) -> [MarkdownBlock] {
/// How many blockquotes deep a quote draws. Each level draws the view again inside the last, so text quoted
/// thousands of times would exhaust the stack.
static let maxQuoteDepth = 8

/// The blocks of `markdown`, which sits `quoteDepth` blockquotes deep. A code fence that isn't closed yet, as while an
/// answer arrives, runs to the end, and a table shows as text until its second line marks it as one. At
/// `maxQuoteDepth`, quoted lines stay text, markers included.
static func blocks(_ markdown: String, quoteDepth: Int = 0) -> [MarkdownBlock] {
var blocks: [MarkdownBlock] = []
// The lines of a paragraph, or of a table when they start with a pipe.
var paragraph: [Substring] = []
Expand All @@ -35,7 +40,7 @@ enum MarkdownBlock: Equatable {
quote = []
}
for (index, line) in markdown.split(separator: "\n", omittingEmptySubsequences: false).enumerated() {
if code == nil, let quoted = line.wholeMatch(of: /[ ]{0,3}>[ \t]?(.*)/) {
if code == nil, quoteDepth < maxQuoteDepth, let quoted = line.wholeMatch(of: /[ ]{0,3}>[ \t]?(.*)/) {
endParagraph()
quote.append(quoted.1)
continue
Expand Down Expand Up @@ -118,17 +123,19 @@ enum MarkdownBlock: Equatable {
/// Markdown drawn block by block: paragraphs, headings, blockquotes, lists, code and tables.
struct MarkdownBlocks: View {
let markdown: String
/// How many blockquotes this text sits in.
var quoteDepth = 0

var body: some View {
VStack(alignment: .leading, spacing: 8) {
ForEach(Array(MarkdownBlock.blocks(markdown).enumerated()), id: \.offset) { _, block in
ForEach(Array(MarkdownBlock.blocks(markdown, quoteDepth: quoteDepth).enumerated()), id: \.offset) { _, block in
switch block {
case let .paragraph(text):
Self.inline(text)
case let .heading(text):
Self.inline(text).fontWeight(.semibold)
case let .blockquote(text):
MarkdownBlocks(markdown: text)
MarkdownBlocks(markdown: text, quoteDepth: quoteDepth + 1)
.padding(.leading, 10)
.overlay(alignment: .leading) {
Rectangle().fill(.foreground.opacity(0.3)).frame(width: 2)
Expand Down
20 changes: 20 additions & 0 deletions Tests/StableBarTests/DefaultLayoutTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,26 @@ func listEntriesDrawInOrderWithTheirSpacing(direction: (property: String, type:
#expect(MarkdownBlock.blocks("> ~~~\n> code\nOutside") == [.blockquote("~~~\ncode"), .paragraph("Outside")])
}

@Test func markdownBlockquotesStopNestingAtTheLimit() {
let levels = 10_000
var blocks = MarkdownBlock.blocks(String(repeating: "> ", count: levels) + "deep\nlazy")
var depth = 0
while blocks.count == 1, case let .blockquote(text) = blocks[0] {
depth += 1
blocks = MarkdownBlock.blocks(text, quoteDepth: depth)
}
#expect(depth == MarkdownBlock.maxQuoteDepth)
#expect(blocks == [.paragraph(String(repeating: "> ", count: levels - depth) + "deep\nlazy")],
"Past the limit, the rest of the quote is text")
#expect(MarkdownBlock.blocks("> a\n\nb", quoteDepth: MarkdownBlock.maxQuoteDepth) == [.paragraph("> a"), .paragraph("b")])
}

@Test @MainActor func deeplyNestedBlockquotesDraw() throws {
let markdown = String(repeating: "> ", count: 5_000) + "deep"
let drawing = try draw(MarkdownBlocks(markdown: markdown).frame(width: 300, alignment: .leading))
#expect(drawing.width == 600)
}

/// A drawing is one frame, so this checks the still shapes: playing in the measuring copy and under
/// Reduce Motion, and settled when the source is off.
@Test @MainActor func equalizersDrawStillWhileMeasuredAndSettleWhenTheirSourceIsOff() throws {
Expand Down
2 changes: 1 addition & 1 deletion docs/popups.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Layout blocks:
| `row` | `children` | Blocks arranged horizontally |
| `column` | `children` | Blocks arranged vertically |
| `text` | `text` | Static text |
| `field` | `field` | A string from the script's `fields` object, or the current list row. With `"wrap": true` it shows all of the text over as many lines as it needs, and the text can be selected and copied; `maxHeight` scrolls a wrapping field past that height in points and keeps its newest lines in view as the text grows, until you scroll away. `"markdown": true` shows Markdown: bold, italics, `code` and links, and in a field that wraps also headings, blockquotes, lists, code blocks and tables, as an AI app shows an answer. |
| `field` | `field` | A string from the script's `fields` object, or the current list row. With `"wrap": true` it shows all of the text over as many lines as it needs, and the text can be selected and copied; `maxHeight` scrolls a wrapping field past that height in points and keeps its newest lines in view as the text grows, until you scroll away. `"markdown": true` shows Markdown: bold, italics, `code` and links, and in a field that wraps also headings, blockquotes, lists, code blocks and tables, as an AI app shows an answer. Blockquotes nest up to 8 deep; a quote inside those shows as text, its `>` markers included. |
| `graph` | `height` | The widget's existing 60 samples on the 0–1 scale, the newest at the right edge; optional `source` reads `series[source]` instead and `lineWidth` sets stroke width |
| `list` | `source`, `row` | Repeat the `row` block for each object in `lists[source]`, in source order. `orientation` is `vertical` by default (also when null); set `"orientation": "horizontal"` to place entries side by side. `style.spacing` separates entries in either direction. Horizontal lists in popups scroll when their entries exceed the available width; `maxHeight` scrolls a vertical list past that height in points, as a chat does: it opens at its end, follows what grows there until you scroll away, and brings a new entry into view. |
| `button` | `title`, `onClick` | Run a command and close the popup |
Expand Down
38 changes: 38 additions & 0 deletions scenes/markdown-deep-quote-popup.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"$schema": "../scene.schema.json",
"configuration": {
"widgets": [
{
"id": "notes",
"type": "script",
"command": "true",
"position": "right",
"icon": "text.quote",
"popup": {
"style": {
"width": 320
},
"layout": {
"type": "field",
"field": "answer",
"wrap": true,
"markdown": true
}
}
}
]
},
"outputs": {
"notes": {
"text": "",
"icon": "text.quote",
"fields": {
"answer": "A reply quoted ten times:\n\n> > > > > > > > > > **Deepest** reply"
}
}
},
"appearances": ["dark"],
"draw": "popup",
"widget": "notes",
"height": 200
}
Loading