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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!-- This HTML fragment will be inserted into the `<head>` element when the Markdown page -->
<!-- includes `fragment: head: ['example']` in the frontmatter. -->
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
<script src="${ASSET-search.js}"></script>
<script src="${ASSET-support.js}"></script>

<!-- {HEAD FRAGMENTS} -->

<script>
function onSearch(searchTerm) {
performSearch(searchTerm, '${BASE_PATH}');
Expand Down
5 changes: 5 additions & 0 deletions examples/sample-docs/projects/sample-guide/content/page_1.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
---
fragments:
head: ['example']
---

# <!-- section:page_1 --><!-- def:title -->Page 1

<!-- def:intro -->
Expand Down
1 change: 1 addition & 0 deletions packages/docs-builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"find-up": "^6.3.0",
"gettext-parser": "^5.0.0",
"glob": "^8.0.3",
"gray-matter": "^4.0.3",
"lunr": "^2.3.9",
"lunr-languages": "^1.9.0",
"mark.js": "^8.11.1",
Expand Down
18 changes: 16 additions & 2 deletions packages/docs-builder/src/gen-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,17 @@ export function generateHtml(context: Context, mdRelPath: string, mdPage: Markdo
// Convert the Markdown content to HTML
const body = convertMarkdownToHtml(context, md)

// Save the names of the `<head>` fragments to include
const headFragments = mdPage.frontmatter?.fragments?.head || []

// Clear the current page
context.setCurrentPage(undefined)

return {
baseName,
relPath: htmlRelPath,
body
body,
headFragments
}
}

Expand Down Expand Up @@ -308,7 +312,17 @@ export function writeHtmlFile(
// Build the final HTML file by replacing fields in the template
const templateFile = `template-${templateName}.html`
const templatePath = resolvePath(context.config.sourceDir, templateFile)
const htmlTemplate = readTextFile(templatePath)
const htmlTemplateRaw = readTextFile(templatePath)
let headSlot = ''
if (htmlPage.headFragments) {
// Insert fragments into the `<head>` slot
for (const fragmentName of htmlPage.headFragments) {
const fragmentPath = resolvePath(context.config.sourceDir, `fragment-${fragmentName}.html`)
const fragmentHtml = readTextFile(fragmentPath)
headSlot += fragmentHtml + '\n'
}
}
const htmlTemplate = htmlTemplateRaw.replace('<!-- {HEAD FRAGMENTS} -->', headSlot)
const html = htmlTemplate.replaceAll(/\${([a-zA-Z0-9._-]*)}/g, (_substring, id) => {
if (id.startsWith('ASSET-')) {
// Assets are specified like `${ASSET-my-image.png}`; remap to the full relative path to
Expand Down
12 changes: 10 additions & 2 deletions packages/docs-builder/src/parse.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Copyright (c) 2022 Climate Interactive / New Venture Fund

import { resolve } from 'path'
import matter from 'gray-matter'
import { marked } from 'marked'

import type { BlockId } from './block'
import type { Command } from './command'
import type { Context } from './context'
Expand Down Expand Up @@ -46,7 +48,12 @@ export function parseMarkdownPage(context: Context, relPath: string): MarkdownPa

// Read the Markdown file
const filePath = resolve(context.config.baseProjDir, relPath)
const origMarkdown = readTextFile(filePath)
const origMarkdownWithFrontmatter = readTextFile(filePath)

// Separate frontmatter from the content
const origMarkdownSeparated = matter(origMarkdownWithFrontmatter)
const origMarkdown = origMarkdownSeparated.content
const frontmatter = origMarkdownSeparated.data

// Append synthesized link info for glossary references so that source files can
// use `[link text][glossary_term]` without manually defining a reference for
Expand Down Expand Up @@ -81,7 +88,8 @@ export function parseMarkdownPage(context: Context, relPath: string): MarkdownPa
context.setCurrentPage(undefined)

return {
raw: outMarkdown
raw: outMarkdown,
frontmatter
}
}

Expand Down
8 changes: 8 additions & 0 deletions packages/docs-builder/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@

export type LangCode = string

export type Frontmatter = {
fragments?: {
head?: string[]
}
}

export interface MarkdownPage {
raw: string
frontmatter?: Frontmatter
}

export interface HtmlPage {
baseName: string
relPath: string
body: string
headFragments?: string[]
}
67 changes: 67 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.