Skip to content
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

Allow usage front-matter data in listing template #348

Merged
merged 1 commit into from
Dec 6, 2020
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
27 changes: 25 additions & 2 deletions lib/listing.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,41 @@
const Mustache = require('mustache');
const path = require('path');
const { getInitialDir, getOptions, getListingTemplate, getThemeUrl, getFilesGlob } = require('./config');
const { listFiles } = require('./util');
const { listFiles, parseYamlFrontMatter } = require('./util');
const fs = require('fs-extra');


const listFilesMeta = async (files) => {
const baseDir = await getInitialDir();

return Promise.all(files.map(async filePath => {
const markdownFilePath = path.join(baseDir, filePath).replace('.html', '.md');
const markdown = await fs.readFile(markdownFilePath);
let yamlOptions = {};
try {
yamlOptions = parseYamlFrontMatter(markdown.toString()).yamlOptions;
} catch(e) {
// there might be no front matter info in the file
}
return {
filePath,
yamlOptions
}
}));
}

const renderListFile = async files => {
const { title, listingTemplate, theme } = getOptions();
const template = await getListingTemplate(listingTemplate);
const themeUrl = getThemeUrl(theme, '.');
const extendedFileRecords = await listFilesMeta(files);
return Mustache.render(template, {
base: '',
themeUrl,
title,
filePaths: files,
fileNames: files.map(file => path.basename(file))
fileNames: files.map(file => path.basename(file)),
extendedFileRecords,
});
};

Expand Down
8 changes: 5 additions & 3 deletions lib/template/listing.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@

<body>
<ul>
{{#filePaths}}
<li><a href="{{.}}">{{.}}</a></li>
{{/filePaths}}
{{#extendedFileRecords}}
{{#.}}
<li><a href="{{filePath}}" title="{{yamlOptions.title}}">{{filePath}}</a></li>
{{/.}}
{{/extendedFileRecords}}
</ul>
</body>
</html>