Skip to content
Open
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,4 @@ __artifacts__

.DS_Store
tests_output/
nightwatch/
nightwatch/
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,36 @@ and then run `npx postdoc init` to create a new Postdoc project.

Visit [the official documentation](https://postdoc.dev).



```shell
_____ _ _
| __ \ | | | |
| |__) |__ ___| |_ __| | ___ ___
| ___/ _ \/ __| __/ _` |/ _ \ / __|
| | | (_) \__ \ || (_| | (_) | (__
|_| \___/|___/\__\__,_|\___/ \___|


───────────────────────────────────────

Usage: postdoc [options] [command]

Options:
--version / -v outputs the current Postdoc version.
--help / -h display help for command

Commands:
run Start the Vite-powered development server with live preview and Hot Module Replacement (HMR).
init Initializes a new Postdoc project and copies the necessary assets to start with.
test Runs all test declared in the project.
build Builds the project, copies assets into an output directory.
import Creates new pages and minimal tests from a CLI project.
create
preview Starts a simple static server over the output directory.

```

## Support

Having trouble? Get help in the [Discussions](https://github.com/PostDocJS/postdoc/discussions) or [create an issue](https://github.com/PostDocJS/postdoc/issues).
Expand Down
2 changes: 1 addition & 1 deletion jsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"compilerOptions": {
"moduleResolution": "node16"
},
"include": ["lib/**/*", "bin/*", "client/*"]
"include": ["lib/**/*", "bin/*", "client/*", "documentation"]
}
35 changes: 31 additions & 4 deletions lib/collector.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@ import { pipeWith } from 'pipe-ts';
import Logger from './logger.js';
import ApiPage from './api-page.js';
import LayoutPage from './layout-page.js';
import RegularPage from './regular-page.js';
import MarkdownPage from './markdown-page.js';
import RSTPage from './rst-page.js';
import PostDocError from './error.js';
import Configuration from './configuration.js';
import MarkdownCompiler from './markdown-compiler.js';
import RSTCompiler from './rst-compiler.js';
import { walkDirectory } from './fs.js';

export default class Collector {
#pages = [];
#configuration = Configuration.get();
#markdownCompiler = new MarkdownCompiler();
#rstCompiler = new RSTCompiler();
#temporaryDirectoryPrefix = '.pd-tmp-';
#temporaryOutputDirectoryPath;
#isTemporaryOutputDirectoryPathVirtual;
Expand Down Expand Up @@ -84,7 +87,7 @@ export default class Collector {
);
}

#collectRegularPages() {
#collectMDPages() {
const shouldIgnore = anymatch(this.#configuration.ignore.pages);

const pages = this.#configuration.directories.content;
Expand All @@ -97,7 +100,30 @@ export default class Collector {
}),
AsyncIterable.filter((filePath) => filePath.endsWith('.md')),
AsyncIterable.filter((filePath) => !shouldIgnore(filePath)),
AsyncIterable.map((filePath) => this.#tryCreatingPage(filePath, RegularPage, markdownCompiler)),
AsyncIterable.map((filePath) => this.#tryCreatingPage(filePath, MarkdownPage, markdownCompiler)),
AsyncIterable.filter(Boolean),
AsyncIterable.chain((page) => AsyncIterable.from(async function* () {
if (await page.shouldCompile()) {
yield page;
}
}))
);
}

#collectRSTPages() {
const shouldIgnore = anymatch(this.#configuration.ignore.pages);

const pages = this.#configuration.directories.content;
const rstCompiler = this.#rstCompiler;

return pipeWith(AsyncIterable.from(async function* () {
await rstCompiler.initialise();

yield* pipeWith(pages, resolve, walkDirectory, ({ files }) => files);
}),
AsyncIterable.filter((filePath) => filePath.endsWith('.rst')),
AsyncIterable.filter((filePath) => !shouldIgnore(filePath)),
AsyncIterable.map((filePath) => this.#tryCreatingPage(filePath, RSTPage, rstCompiler)),
AsyncIterable.filter(Boolean),
AsyncIterable.chain((page) => AsyncIterable.from(async function* () {
if (await page.shouldCompile()) {
Expand Down Expand Up @@ -129,7 +155,8 @@ export default class Collector {
const layoutsUsedInPages = new Set();

const pages = await pipeWith(
this.#collectRegularPages(),
this.#collectMDPages(),
AsyncIterable.concat(this.#collectRSTPages()),
AsyncIterable.concat(this.#collectApiDocs()),
AsyncIterable.fold([], (pages, page) => {
pages.push(page);
Expand Down
4 changes: 2 additions & 2 deletions lib/regular-page.js → lib/markdown-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Page from './page.js';
import Logger from './logger.js';
import Configuration from './configuration.js';

export default class RegularPage extends Page {
export default class MarkdownPage extends Page {
static #findLayout(directoryPath, contentPageName, configuration) {
const absoluteLayoutsPath = resolve(configuration.directories.layouts);

Expand Down Expand Up @@ -41,7 +41,7 @@ export default class RegularPage extends Page {
const rootPagesDirectoryPath = resolve(configuration.directories.content);

const outputFilePath = Page.resolveOutputFilePath(contentFilePath, rootPagesDirectoryPath, temporaryOutputDirectoryName);
const layoutFilePath = RegularPage.#findLayout(dirname(contentFilePath.replace(rootPagesDirectoryPath, resolve(configuration.directories.layouts))), contentPageName, configuration);
const layoutFilePath = MarkdownPage.#findLayout(dirname(contentFilePath.replace(rootPagesDirectoryPath, resolve(configuration.directories.layouts))), contentPageName, configuration);

super(layoutFilePath, outputFilePath, temporaryOutputDirectoryName);

Expand Down
67 changes: 67 additions & 0 deletions lib/rst-compiler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import fm from 'front-matter';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import child_process from 'node:child_process';
import Configuration from './configuration.js';

const INTERPOLATION_RE = /{{([^}]+)}}/g;


class RSTToHTML {
async parse(rstText){

// Needs pandoc installed
// https://github.com/jgm/pandoc/releases
// pandoc index.rst -f rst -t html -o index.html

const rstFilePath = path.join(os.tmpdir(), "file.rst")
fs.writeFileSync(rstFilePath, rstText, 'utf-8')
const htmlFilePath = path.join(os.tmpdir(), "file.html")
const command = `pandoc ${rstFilePath} -f rst -t html -o ${htmlFilePath}`
child_process.exec(command, (error, out, err) => { return })
const htmlContent = fs.readFileSync(htmlFilePath, 'utf-8')
fs.unlinkSync(htmlFilePath)

if (!htmlContent) {
throw Error("Could not compile rst file to html")
}

return htmlContent

}
}


export default class RSTCompiler {
#compiler;

async initialise() {
if (this.#compiler) {
return;
}

this.#compiler = new RSTToHTML();

}

async compile(content) {
const { attributes, body } = fm(content);

const html = await this.#compiler.parse(body);

const configuration = Configuration.get();

return [attributes, html.replace(INTERPOLATION_RE, (_, variableName) => {
const trimmedVariableName = variableName.trim();

if (trimmedVariableName.startsWith('appSettings')) {
const properties = trimmedVariableName.split('.').slice(1);

return properties.reduce((target, property) => target[property.trim()], configuration.appSettings);
}

return attributes[trimmedVariableName];
})];
}
}
85 changes: 85 additions & 0 deletions lib/rst-page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { inspect } from 'node:util';
import { readFile } from 'node:fs/promises';
import { existsSync } from 'node:fs';
import { basename, dirname, extname, join, resolve } from 'node:path';

import Page from './page.js';
import Logger from './logger.js';
import Configuration from './configuration.js';

export default class RSTPage extends Page {
static #findLayout(directoryPath, contentPageName, configuration) {
const absoluteLayoutsPath = resolve(configuration.directories.layouts);

if (!directoryPath.includes(absoluteLayoutsPath)) {
return;
}

const namedLayoutPath = join(directoryPath, contentPageName + '.ejs');
const sharedLayoutPath = join(directoryPath, 'index.ejs');

if (existsSync(namedLayoutPath)) {
return namedLayoutPath;
}

if (existsSync(sharedLayoutPath)) {
return sharedLayoutPath;
}

if (directoryPath !== absoluteLayoutsPath) {
return this.#findLayout(join(directoryPath, '..'), contentPageName, configuration);
}
}

#contentFilePath;
#rstCompiler;

constructor(contentFilePath, temporaryOutputDirectoryName, rstCompiler) {
const configuration = Configuration.get();

const contentPageName = basename(contentFilePath, extname(contentFilePath));
const rootPagesDirectoryPath = resolve(configuration.directories.content);

const outputFilePath = Page.resolveOutputFilePath(contentFilePath, rootPagesDirectoryPath, temporaryOutputDirectoryName);
const layoutFilePath = RSTPage.#findLayout(dirname(contentFilePath.replace(rootPagesDirectoryPath, resolve(configuration.directories.layouts))), contentPageName, configuration);

super(layoutFilePath, outputFilePath, temporaryOutputDirectoryName);

this.#contentFilePath = contentFilePath;
this.#rstCompiler = rstCompiler;
}

async #compileContent(allowDrafts) {
const content = await readFile(this.#contentFilePath, 'utf8');

try {
const result = await this.#rstCompiler.compile(content);

if (result[0].draft && !allowDrafts) {
return;
}

return result;
} catch (error) {
Logger.log((typography) => `
Cannot compile the ${typography.bold(this.#contentFilePath)} because of the following error:
${inspect(error, { compact: false, colors: true })}
`, Logger.ErrorLevel);
}
}

async compile(pages, allowDrafts) {
const compiledContentAndFrontMatter = await this.#compileContent(allowDrafts);

if (!compiledContentAndFrontMatter) {
// All relative errors have been displayed. Just abort writing.
return;
}

const [frontMatter, content] = compiledContentAndFrontMatter;

return this.compileLayout(pages, {
...frontMatter, content
});
}
}
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"globals": "^13.24.0",
"mock-fs": "^5.2.0",
"mock-require": "^3.0.3",
"sinon": "^15.0.1",
"sinon": "^17.0.1",
"tree-kill": "^1.2.2"
},
"dependencies": {
Expand Down