Skip to content

Handle empty files and truncated YAML front matter #17527

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 17, 2023
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
11 changes: 8 additions & 3 deletions scaladoc/src/dotty/tools/scaladoc/site/common.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,16 @@ def yamlParser(using ctx: StaticSiteContext): Parser = Parser.builder(defaultMar
def loadTemplateFile(file: File, defaultTitle: Option[TemplateName] = None)(using ctx: StaticSiteContext): TemplateFile = {
val lines = Files.readAllLines(file.toPath).asScala.toList

val (config, content) = if (lines.head == ConfigSeparator) {
val (config, content) = if (!lines.isEmpty && lines.head == ConfigSeparator) {
// Taking the second occurrence of ConfigSeparator.
// The rest may appear within the content.
val index = lines.drop(1).indexOf(ConfigSeparator) + 2
(lines.take(index), lines.drop(index))
val secondSeparatorIndex = lines.drop(1).indexOf(ConfigSeparator)
if secondSeparatorIndex != -1 then
(lines.take(secondSeparatorIndex + 2), lines.drop(secondSeparatorIndex + 2))
else
// If there is no second occurrence of ConfigSeparator, we assume that the
// whole file is config.
(lines.tail, Nil)
} else (Nil, lines)

val configParsed = yamlParser.parse(config.mkString(LineSeparator))
Expand Down
Empty file.
3 changes: 3 additions & 0 deletions scaladoc/test-documentations/noConfigEnd/_docs/hello.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: My page
foo: bar
16 changes: 16 additions & 0 deletions scaladoc/test/dotty/tools/scaladoc/site/SiteGeneratationTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,22 @@ class SiteGeneratationTest extends BaseHtmlTest:
testApiPages(mainTitle = projectName, parents = Nil, hasToplevelIndexIndex = false)
}

@Test
def emptyPage() = withGeneratedSite(testDocPath.resolve("emptyPage")){
withHtmlFile("docs/hello.html") { content =>
// There should be no content as the page body is empty.
content.assertTextsIn("#content", Nil*)
}
}

@Test
def noConfigEnd() = withGeneratedSite(testDocPath.resolve("noConfigEnd")){
withHtmlFile("docs/hello.html") { content =>
// There should be no content as the page body is empty.
content.assertTextsIn("#content", Nil*)
}
}

@Test
def staticLinking() = withGeneratedSite(testDocPath.resolve("static-links")){

Expand Down