Skip to content

Commit

Permalink
Allow usage front-matter data in listing template
Browse files Browse the repository at this point in the history
  • Loading branch information
vvscode committed Nov 29, 2020
1 parent 5eb2576 commit e247432
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
25 changes: 23 additions & 2 deletions lib/listing.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,39 @@
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');


const listFilesMeta = async (files) => {
return Promise.all(files.map(async filePath => {
const baseDir = await getInitialDir();
const markdown = fs.readFileSync(path.join(baseDir, filePath).replace('.html', '.md')).toString();
let yamlOptions = {};
try {
yamlOptions = parseYamlFrontMatter(markdown).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>

0 comments on commit e247432

Please sign in to comment.