Skip to content

Commit 0d5e556

Browse files
committed
Render formatted code in quote blocks
1 parent 91e7cb0 commit 0d5e556

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

src/markdown/generator.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ and item (l : Item.t list) args nesting_level =
316316
let code, _, content = source_take_until_punctuation code in
317317
let content =
318318
if source_contains_text content then
319-
paragraph (source_code content args)
319+
quote_block (paragraph (source_code content args))
320320
else noop
321321
in
322322
render_declaration ~anchor ~doc code content

src/markdown/markup.ml

+12
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ and blocks =
2020
| List of list_type * blocks list
2121
| Raw_markup of string
2222
| Block_separator
23+
| Prefixed_block of string * blocks (** Prefix every lines of blocks. *)
2324

2425
and list_type = Ordered | Unordered
2526

@@ -64,11 +65,20 @@ let paragraph i = Block i
6465

6566
let code_block s = CodeBlock s
6667

68+
let quote_block b = Prefixed_block ("> ", b)
69+
6770
let heading level i =
6871
let make_hashes n = String.make n '#' in
6972
let hashes = make_hashes level in
7073
Block (String hashes ++ i)
7174

75+
(** Every lines that [f] formats are prefixed and written in [sink].
76+
Inefficient. *)
77+
let with_prefixed_formatter prefix sink f =
78+
let s = Format.asprintf "%t" f in
79+
String.split_on_char '\n' s
80+
|> List.iter (fun l -> Format.fprintf sink "%s%s@\n" prefix l)
81+
7282
let pp_list_item fmt list_type (b : blocks) n pp_blocks =
7383
match list_type with
7484
| Unordered -> Format.fprintf fmt "- @[%a@]" pp_blocks b
@@ -109,3 +119,5 @@ let rec pp_blocks fmt b =
109119
in
110120
pp_list 0 l
111121
| Raw_markup s -> Format.fprintf fmt "%s" s
122+
| Prefixed_block (p, b) ->
123+
with_prefixed_formatter p fmt (fun fmt -> pp_blocks fmt b)

src/markdown/markup.mli

+2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ val paragraph : inlines -> blocks
5555

5656
val code_block : string -> blocks
5757

58+
val quote_block : blocks -> blocks
59+
5860
val heading : int -> inlines -> blocks
5961

6062
val pp_blocks : Format.formatter -> blocks -> unit

test/integration/markdown.t/run.t

+8-4
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,17 @@
3434

3535
###### val x :
3636

37-
[t](#type-t)
37+
> [t](#type-t)
38+
3839

3940
Doc for `val x`
4041

4142
<a id="type-a"></a>
4243

4344
###### type a =
4445

45-
[t](#type-t)
46+
> [t](#type-t)
47+
4648

4749
Type alias
4850

@@ -88,7 +90,8 @@
8890

8991
###### val y :
9092

91-
[ `One | `Two ]
93+
> [ `One | `Two ]
94+
9295

9396
Polymorphic variant.
9497

@@ -98,7 +101,8 @@
98101

99102
###### val z :
100103

101-
[t](#type-t) -> ( [t](#type-t) -> [t](#type-t) ) -> foo : [t](#type-t) -> ? bar : [t](#type-t) -> [ `One of [t](#type-t) ] -> [t](#type-t) * [t](#type-t)
104+
> [t](#type-t) -> ( [t](#type-t) -> [t](#type-t) ) -> foo : [t](#type-t) -> ? bar : [t](#type-t) -> [ `One of [t](#type-t) ] -> [t](#type-t) * [t](#type-t)
105+
102106

103107
Type complicated enough to be rendered differently.
104108

0 commit comments

Comments
 (0)