Skip to content

Commit 2ceafcb

Browse files
committed
Update setup questions
1 parent d0bf6f4 commit 2ceafcb

File tree

4 files changed

+31
-22
lines changed

4 files changed

+31
-22
lines changed

index.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,13 @@ module.exports.getOptionsFromSetup = ({ answers, debug }) => {
5858
if (page.location.fileName) {
5959
location = `'${page.location.fileName}'`;
6060
} else {
61-
const { directory, fileNameField, useDate } = page.location;
61+
const { directory, fileNameField } = page.location;
6262
const locationParts = [];
6363

6464
if (directory) {
6565
locationParts.push(`'${directory}/'`);
6666
}
6767

68-
if (useDate) {
69-
locationParts.push(`createdAt.substring(0, 10) + '-'`);
70-
}
71-
7268
locationParts.push(`utils.slugify(fields['${fileNameField}']) + '.md'`);
7369
location = locationParts.join(" + ");
7470
}
@@ -81,7 +77,7 @@ module.exports.getOptionsFromSetup = ({ answers, debug }) => {
8177
? `'${page.layout}'`
8278
: `fields['${page.layout}']`;
8379
const extractedProperties = [
84-
"__metadata",
80+
"__metadata = {}",
8581
page.contentField ? `'${page.contentField}': content` : null,
8682
page.layoutSource ? "layout" : null,
8783
"...frontmatterFields"
@@ -91,11 +87,19 @@ module.exports.getOptionsFromSetup = ({ answers, debug }) => {
9187
projectId && `projectId === '${projectId}'`,
9288
source && `source === '${source}'`
9389
].filter(Boolean);
90+
let addDate = "";
91+
92+
if (page.addDateField) {
93+
addDate = `
94+
if (typeof entry.__metadata.createdAt === 'string') {
95+
frontmatterFields.date = entry.__metadata.createdAt.split('T')[0]
96+
}\n`;
97+
}
9498

9599
conditions.push(
96100
`if (${conditionParts.join(" && ")}) {`,
97101
` const { ${extractedProperties.filter(Boolean).join(", ")} } = entry;`,
98-
``,
102+
addDate,
99103
` return {`,
100104
` content: {`,
101105
` body: ${contentField},`,

lib/setup.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ module.exports.getSetupForPage = async ({
209209
? { fileName: locationValue }
210210
: { fileNameField: locationValue };
211211
} else {
212-
const { directory, fileNameField, useDate } = await inquirer.prompt([
212+
const { directory, fileNameField } = await inquirer.prompt([
213213
{
214214
type: "list",
215215
name: "directory",
@@ -227,15 +227,6 @@ module.exports.getSetupForPage = async ({
227227
when: ({ directory }) => directory === null,
228228
message: "Insert the location for this collection."
229229
},
230-
{
231-
type: "confirm",
232-
name: "useDate",
233-
message: ({ directory }) =>
234-
`Do you want to use the post date in the file name? ${chalk.reset.dim(
235-
`(e.g. '${directory}/2018-03-56-my-blog-post.md')`
236-
)}`,
237-
default: true
238-
},
239230
{
240231
type: "list",
241232
name: "fileNameField",
@@ -256,12 +247,16 @@ module.exports.getSetupForPage = async ({
256247

257248
answers.location = {
258249
directory,
259-
fileNameField,
260-
useDate
250+
fileNameField
261251
};
262252
}
263253

264-
const { contentField, layout, layoutSource } = await inquirer.prompt([
254+
const {
255+
addDateField,
256+
contentField,
257+
layout,
258+
layoutSource
259+
} = await inquirer.prompt([
265260
{
266261
type: "list",
267262
name: "layoutSource",
@@ -299,6 +294,15 @@ module.exports.getSetupForPage = async ({
299294
name: "layout",
300295
message: "Insert the layout name"
301296
},
297+
{
298+
type: "confirm",
299+
name: "addDateField",
300+
message: ({ directory }) =>
301+
`Do you want to add a 'date' field to the frontmatter? ${chalk.reset.dim(
302+
`(e.g. field: 2019-12-31)`
303+
)}`,
304+
default: true
305+
},
302306
{
303307
type: "list",
304308
name: "contentField",
@@ -321,6 +325,7 @@ module.exports.getSetupForPage = async ({
321325
}
322326
]);
323327

328+
answers.addDateField = addDateField;
324329
answers.contentField = contentField;
325330
answers.layout = layout;
326331
answers.layoutSource = layoutSource;

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sourcebit-target-hugo",
3-
"version": "0.1.1",
3+
"version": "0.2.0",
44
"description": "A Sourcebit plugin for Hugo",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)