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
9 changes: 4 additions & 5 deletions basic/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import {dirname, relative, join} from 'node:path';
import {getPages, allPages, writeFile} from '@sphido/core';
import slugify from '@sindresorhus/slugify';
import {marked} from 'marked';
import {frontmatter} from '@sphido/frontmatter';
import {markdown} from '@sphido/markdown';

function getHtml({title, content, path, date}) {
return `<!DOCTYPE html>
Expand All @@ -23,15 +23,14 @@ function getHtml({title, content, path, date}) {
</html>`;
}

const pages = await getPages({path: 'content'}, frontmatter);
// frontmatter loads page.content and reads the YAML block, markdown() renders
// the rest to HTML — it goes last, after every extender that wants markdown
const pages = await getPages({path: 'content'}, frontmatter, markdown());

for (const page of allPages(pages)) {
page.slug = slugify(page.name) + '.html';
page.output = join('public', relative('content', dirname(page.path)), page.slug);

// process markdown (PS: frontmatter already loads page.content)
page.content = marked(page.content);

// save HTML file
await writeFile(page.output, getHtml(page));
}
2 changes: 1 addition & 1 deletion basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"@sindresorhus/slugify": "2.2.1",
"@sphido/core": "^3.1.0",
"@sphido/frontmatter": "^3.1.0",
"marked": "^18.0.0"
"@sphido/markdown": "^1.0.0"
},
"devDependencies": {
"serve": "14.2.4"
Expand Down
14 changes: 6 additions & 8 deletions extenders/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env node

import {dirname, relative, join} from 'node:path';
import {getPages, allPages, writeFile, readFile} from '@sphido/core';
import {getPages, allPages, writeFile} from '@sphido/core';
import slugify from '@sindresorhus/slugify';
import {marked} from 'marked';
import {markdown} from '@sphido/markdown';


function metadata(page, dirent) {
Expand All @@ -17,10 +17,6 @@ function author(page) {
page.author = 'Roman Ožana';
}

async function getContent() {
return marked(await readFile(this.path));
}

const pages = await getPages({path: 'content'},

// callback extender
Expand All @@ -30,9 +26,11 @@ const pages = await getPages({path: 'content'},
page.date = new Date();
},

// extender package: renders page.content from markdown to HTML
markdown(),

// Object extenders
{
getContent,
getFooter: function () {
return `<footer class="border-t text-sm text-gray-500 pt-1 mt-1 text-right">Generated from file ${this.path}</footer>`;
},
Expand All @@ -51,7 +49,7 @@ const pages = await getPages({path: 'content'},
</head>
<body class="prose mx-auto my-6">
<header class="border-b text-2xl py-2 mb-4">Sphido Example website</header>
<main>${await this.getContent()}</main>
<main>${this.content}</main>
${this.getFooter()}
</body>
</html>`;
Expand Down
2 changes: 1 addition & 1 deletion extenders/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"dependencies": {
"@sindresorhus/slugify": "latest",
"@sphido/core": "^3.1.0",
"marked": "^18.0.0"
"@sphido/markdown": "^1.0.0"
},
"devDependencies": {
"serve": "latest"
Expand Down
7 changes: 3 additions & 4 deletions json/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
import {dirname, relative, join} from 'node:path';
import {getPages, allPages, writeFile} from '@sphido/core';
import slugify from '@sindresorhus/slugify';
import {marked} from 'marked';
import {frontmatter} from '@sphido/frontmatter';
import {markdown} from '@sphido/markdown';

const pages = await getPages({path: 'content'}, frontmatter);
const pages = await getPages({path: 'content'}, frontmatter, markdown());

for (const page of allPages(pages)) {
page.slug = slugify(page.name) + '.json';
const output = join('public', relative('content', dirname(page.path)), page.slug);
page.content = marked(page.content);

// save JSON file
// save JSON file with the rendered HTML in page.content
await writeFile(output, JSON.stringify(page));
}
2 changes: 1 addition & 1 deletion json/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
"@sindresorhus/slugify": "2.2.1",
"@sphido/core": "^3.1.0",
"@sphido/frontmatter": "^3.1.0",
"marked": "^18.0.0"
"@sphido/markdown": "^1.0.0"
}
}
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ Here are some examples of how to use [Sphido](https://sphido.org/), a rocket

## [Basic usage](https://github.com/sphido/examples/tree/main/basic)

This example shows how to use Sphido for processing Markdown files to HTML.
This example shows how to use Sphido for processing Markdown files to HTML with the
[`@sphido/markdown`](https://github.com/sphido/sphido/tree/main/packages/sphido-markdown) extender.

## [Extenders](https://github.com/sphido/examples/tree/main/extenders)

Expand Down
8 changes: 4 additions & 4 deletions rss/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env node

import {dirname, relative, join} from 'node:path';
import {getPages, allPages, writeFile, readFile} from '@sphido/core';
import {getPages, allPages, writeFile} from '@sphido/core';
import slugify from '@sindresorhus/slugify';
import {marked} from 'marked';
import {markdown} from '@sphido/markdown';
import {renderFeed, writeFeed} from '@sphido/feed';

function getHtml({title, content, path, date}) {
Expand All @@ -21,14 +21,14 @@ function getHtml({title, content, path, date}) {

const items = [];

const pages = await getPages({path: 'content'});
// markdown() loads each file and renders page.content to HTML
const pages = await getPages({path: 'content'}, markdown());

for (const page of allPages(pages)) {
page.slug = join('/', relative('content', dirname(page.path)), slugify(page.name) + '.html');
page.output = join('public', page.slug);

page.url = new URL(page.slug, 'https://sphido.org/').toString();
page.content = marked(await readFile(page.path));
page.title = page.content.match(/(?<=<h[12][^>]*?>)([^<>]+?)(?=<\/h[12]>)/i)?.pop();
page.date = new Date();

Expand Down
2 changes: 1 addition & 1 deletion rss/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"@sindresorhus/slugify": "2.2.1",
"@sphido/core": "^3.1.0",
"@sphido/feed": "^3.1.0",
"marked": "^18.0.0"
"@sphido/markdown": "^1.0.0"
},
"devDependencies": {
"serve": "14.2.4"
Expand Down
3 changes: 1 addition & 2 deletions sitemap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
"dependencies": {
"@sindresorhus/slugify": "latest",
"@sphido/core": "^3.1.0",
"@sphido/sitemap": "^4.0.0",
"marked": "^18.0.0"
"@sphido/sitemap": "^4.0.0"
},
"devDependencies": {
"serve": "latest"
Expand Down
Loading