Skip to content

Commit 7578a63

Browse files
committed
Fix rendering of .html files from static pages
1 parent 312a420 commit 7578a63

File tree

2 files changed

+71
-43
lines changed

2 files changed

+71
-43
lines changed

scala3doc/src/dotty/dokka/site/templates.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ case class RenderingContext(
2222
properties: Map[String, Object],
2323
layouts: Map[String, TemplateFile] = Map(),
2424
resolving: Set[String] = Set(),
25-
markdownOptions: DataHolder = defaultMarkdownOptions,
2625
resources: List[String] = Nil
2726
):
2827

@@ -82,9 +81,10 @@ case class TemplateFile(
8281
// Library requires mutable maps..
8382
val mutableProperties = new java.util.HashMap[String, Object](ctx.properties.asJava)
8483
val rendered = Template.parse(this.rawCode).render(mutableProperties)
85-
val code = if (!isHtml) rendered else
84+
// We want to render markdown only if next template is html
85+
val code = if (isHtml || layoutTemplate.exists(!_.isHtml)) rendered else
8686
val parser: Parser = Parser.builder().build()
87-
HtmlRenderer.builder(ctx.markdownOptions).build().render(parser.parse(rendered))
87+
HtmlRenderer.builder(defaultMarkdownOptions).build().render(parser.parse(rendered))
8888

8989
val resources = listSetting("extraCSS") ++ listSetting("extraJS")
9090
layoutTemplate match

scala3doc/test/dotty/dokka/site/TemplateFileTests.scala

Lines changed: 68 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,23 @@ class TemplateFileTests:
1515
finally tmpFile.delete()
1616

1717

18-
private def testTemplates(
19-
props: Map[String, String],
20-
template: List[(String, String)])(
21-
op: RenderingContext => Unit
22-
) =
23-
def rec(cxt: RenderingContext, remaining: List[(String, String)]): Unit =
24-
if (remaining.isEmpty) op(cxt)
18+
private def testContent(
19+
expected: String,
20+
props: Map[String, String],
21+
template: List[(String, String)]
22+
) =
23+
def rec(ctx: RenderingContext, remaining: List[(String, String)]): Unit =
24+
if remaining.isEmpty then
25+
assertEquals(expected.trim(), ctx.layouts("content").resolveInner(ctx).code.trim())
2526
else
2627
val (code, ext) = remaining.head
2728
testTemplate(code, ext) { template =>
28-
val newCtx = cxt.copy(layouts = cxt.layouts + (template.name() -> template))
29+
val newCtx = ctx.copy(layouts = ctx.layouts + (template.name() -> template))
2930
rec(newCtx, remaining.drop(1))
3031
}
3132

3233
rec(RenderingContext(props), template)
3334

34-
private def fullRender(template: TemplateFile, ctx: RenderingContext): String = template.resolveInner(ctx).code.trim()
35-
3635
@Test
3736
def testParsingHeaders(): Unit =
3837
testTemplate(
@@ -65,17 +64,14 @@ class TemplateFileTests:
6564
|""".stripMargin
6665

6766

68-
val expected = """<p>Ala ma kota w <strong>paski</strong> from <a href="link/here.md">here</a>. Hej with <a href="link/target.md">link</a>!</p>"""
67+
val expected =
68+
"""<p>Ala ma kota w <strong>paski</strong> from <a href="link/here.md">here</a>. Hej with <a href="link/target.md">link</a>!</p>""".stripMargin
6969

70-
testTemplates(
70+
testContent(
71+
expected,
7172
Map("p1" -> "paski", "p2" -> "Hej"),
72-
List(base -> "html", content -> "md")
73-
) { it =>
74-
assertEquals(
75-
expected,
76-
fullRender(it.layouts("content"), it)
77-
)
78-
}
73+
List(base -> "md", content -> "md")
74+
)
7975

8076
@Test
8177
def layout(): Unit =
@@ -96,25 +92,23 @@ class TemplateFileTests:
9692
|""".stripMargin
9793

9894

99-
val expected = """<p>Ala ma kota w <strong>paski</strong>. Hej!</p>""".stripMargin
95+
val expected =
96+
"""Ala <p>ma kota w <strong>paski</strong></p>
97+
|. Hej!""".stripMargin
10098

101-
testTemplates(
99+
testContent(
100+
expected,
102101
Map("p1" -> "paski", "p2" -> "Hej"),
103102
List(base -> "html", content -> "md")
104-
) { it =>
105-
assertEquals(
106-
expected,
107-
fullRender(it.layouts("content"), it)
108-
)
109-
}
103+
)
110104

111105
@Test
112106
def nestedLayout_htmlMdHtml(): Unit =
113107
val toplevel =
114108
"""---
115109
|name: toplevel
116110
|---
117-
|[div id="root"]{{ content }}[/div]
111+
|<div id="root">{{ content }}</div>
118112
|""".stripMargin
119113

120114
val basePage =
@@ -139,19 +133,20 @@ class TemplateFileTests:
139133

140134

141135
val expected =
142-
"""[div id="root"][h1]Test page[/h1]
143-
|[p]Hello world!![/p]
144-
|[h2]Test page end[/h2]
145-
|[/div]""".stripMargin
136+
"""<div id="root"><h1>Test page</h1>
137+
|<p>Hello world!!</p>
138+
|<h2>Test page end</h2>
139+
|</div>""".stripMargin
146140

147-
testTemplates(
141+
testContent(
142+
expected,
148143
Map("pageName" -> "Test page", "name" -> "world!"),
149144
List(
150145
toplevel -> "html",
151146
basePage -> "md",
152147
content -> "md"
153148
)
154-
) (it => fullRender(it.layouts("content"), it))
149+
)
155150

156151
@Test
157152
def nestedLayout_mdHtmlMd(): Unit =
@@ -187,25 +182,28 @@ class TemplateFileTests:
187182
val expected =
188183
"""<h1>The Page</h1>
189184
|<h2>Test page</h2>
185+
|
190186
|<p>Hello world!!</p>
187+
|
188+
|
191189
|<h3>Test page end</h3>""".stripMargin
192190

193-
testTemplates(
191+
testContent(
192+
expected,
194193
Map("pageName" -> "Test page", "name" -> "world!"),
195194
List(
196195
toplevel -> "html",
197196
basePage -> "html",
198197
content -> "md"
199198
)
200-
) { ctx => assertEquals(expected, fullRender(ctx.layouts("content"), ctx)) }
201-
199+
)
202200
@Test
203201
def markdown(): Unit =
204202
testTemplate(
205203
"""# Hello {{ msg }}!""",
206204
ext = "md"
207205
) { t =>
208-
assertEquals("# Hello there!", t.resolveInner(RenderingContext(Map("msg" -> "there"))).code.trim())
206+
assertEquals("<h1>Hello there!</h1>", t.resolveInner(RenderingContext(Map("msg" -> "there"))).code.trim())
209207
}
210208

211209
@Test
@@ -214,5 +212,35 @@ class TemplateFileTests:
214212
"""# Hello {{ msg }}!""",
215213
ext = "md"
216214
) { t =>
217-
assertEquals("# Hello there!", t.resolveInner(RenderingContext(Map("msg" -> "there"))).code.trim())
218-
}
215+
assertEquals("<h1>Hello there!</h1>", t.resolveInner(RenderingContext(Map("msg" -> "there"))).code.trim())
216+
}
217+
218+
@Test
219+
def htmlOnly(): Unit =
220+
val html =
221+
"""<div>Ala</ala>
222+
|
223+
|<span>Ula</span>
224+
|""".stripMargin
225+
226+
val base =
227+
"""---
228+
|title: myTitle
229+
|name: base
230+
|---
231+
|{{ content }}
232+
|""".stripMargin
233+
234+
val content =
235+
s"""---
236+
|layout: base
237+
|name: content
238+
|---
239+
|$html
240+
|""".stripMargin
241+
242+
243+
testContent(
244+
html,
245+
Map(),
246+
List(base -> "html", content -> "html"))

0 commit comments

Comments
 (0)