Treat placeholder text as markdown

This commit is contained in:
Christoph Hagen 2022-09-02 23:19:30 +02:00
parent d1c418af3e
commit 7f65065f72
2 changed files with 22 additions and 13 deletions

View File

@ -24,8 +24,8 @@ struct PageGenerator {
let inputContentPath = page.path + "/\(language).md" let inputContentPath = page.path + "/\(language).md"
let metadata = page.localized(for: language) let metadata = page.localized(for: language)
let nextLanguage = page.nextLanguage(for: language) let nextLanguage = page.nextLanguage(for: language)
let pageContent = makeContent(page: page, language: language, path: inputContentPath) let (pageContent, pageIncludesCode, pageIsEmpty) = makeContent(
let pageIncludesCode = pageContent?.includesCode ?? false page: page, metadata: metadata, language: language, path: inputContentPath)
var content = [PageTemplate.Key : String]() var content = [PageTemplate.Key : String]()
content[.head] = factory.pageHead.generate(page: page, language: language, includesCode: pageIncludesCode) content[.head] = factory.pageHead.generate(page: page, language: language, includesCode: pageIncludesCode)
@ -36,7 +36,7 @@ struct PageGenerator {
if !page.useCustomHeader { if !page.useCustomHeader {
content[.header] = makeHeader(page: page, metadata: metadata, language: language) content[.header] = makeHeader(page: page, metadata: metadata, language: language)
} }
content[.content] = pageContent?.content ?? factory.makePlaceholder(metadata: metadata) content[.content] = pageContent
content[.previousPageLinkText] = previousPage.unwrapped { factory.factory.html.makePrevText($0.text) } content[.previousPageLinkText] = previousPage.unwrapped { factory.factory.html.makePrevText($0.text) }
content[.previousPageUrl] = previousPage?.link content[.previousPageUrl] = previousPage?.link
content[.nextPageLinkText] = nextPage.unwrapped { factory.factory.html.makeNextText($0.text) } content[.nextPageLinkText] = nextPage.unwrapped { factory.factory.html.makeNextText($0.text) }
@ -51,7 +51,7 @@ struct PageGenerator {
let url = files.urlInOutputFolder(path) let url = files.urlInOutputFolder(path)
if page.state == .draft { if page.state == .draft {
files.isDraft(path: page.path) files.isDraft(path: page.path)
} else if pageContent == nil, page.state != .hidden { } else if pageIsEmpty, page.state != .hidden {
files.isEmpty(page: path) files.isEmpty(page: path)
} }
guard factory.page.generate(content, to: url) else { guard factory.page.generate(content, to: url) else {
@ -60,13 +60,18 @@ struct PageGenerator {
files.generated(page: path) files.generated(page: path)
} }
private func makeContent(page: Element, language: String, path: String) -> (content: String, includesCode: Bool)? { private func makeContent(page: Element, metadata: Element.LocalizedMetadata, language: String, path: String) -> (content: String, includesCode: Bool, isEmpty: Bool) {
guard let content = files.contentOfOptionalFile(atPath: path, source: page.path, createEmptyFileIfMissing: true), if let raw = files.contentOfOptionalFile(atPath: path, source: page.path, createEmptyFileIfMissing: true)?
content.trimmed != "" else { .trimmed.nonEmpty {
return nil let (content, includesCode) = PageContentGenerator(factory: factory.factory)
.generate(page: page, language: language, content: raw)
return (content, includesCode, false)
} else {
let (content, includesCode) = PageContentGenerator(factory: factory.factory)
.generate(page: page, language: language, content: metadata.placeholderText)
let placeholder = factory.makePlaceholder(title: metadata.placeholderTitle, text: content)
return (placeholder, includesCode, true)
} }
return PageContentGenerator(factory: factory.factory)
.generate(page: page, language: language, content: content)
} }
private func makeHeader(page: Element, metadata: Element.LocalizedMetadata, language: String) -> String { private func makeHeader(page: Element, metadata: Element.LocalizedMetadata, language: String) -> String {

View File

@ -75,9 +75,13 @@ struct LocalizedSiteTemplate {
// MARK: Content // MARK: Content
func makePlaceholder(metadata: Element.LocalizedMetadata) -> String { func makePlaceholder(metadata: Element.LocalizedMetadata) -> String {
makePlaceholder(title: metadata.placeholderTitle, text: metadata.placeholderText)
}
func makePlaceholder(title: String, text: String) -> String {
factory.placeholder.generate([ factory.placeholder.generate([
.title: metadata.placeholderTitle, .title: title,
.text: metadata.placeholderText]) .text: text])
} }
func makeBackLink(text: String, language: String) -> String { func makeBackLink(text: String, language: String) -> String {