Craig Fletcher пре 2 недеља
родитељ
комит
02973cb20e
4 измењених фајлова са 105 додато и 29 уклоњено
  1. 5 3
      README.md
  2. 28 2
      src/defaults.js
  3. 45 7
      src/lib.js
  4. 27 17
      src/processors.js

+ 5 - 3
README.md

@@ -156,8 +156,9 @@ inputFiles: [
 
 
 A processor is a function that receives an object with `config` and `meta` properties and returns an object describing what was processed.
 A processor is a function that receives an object with `config` and `meta` properties and returns an object describing what was processed.
 
 
-A processor that returns a `ref` will have it's `detail`, `paths`, `ref` and `fromCache` properties made available in
-`meta.resources`, with the `ref` as the key under the task name:
+A processor that returns a `ref` will have its `detail`, `paths`, `ref` and `fromCache` properties made available in
+`meta.resources`. For expanded tasks (multiple results), the `ref` is used as the key under the task name. For
+single-result tasks, the result is stored directly under the task name:
 ```javascript
 ```javascript
 {
 {
   ...meta,
   ...meta,
@@ -170,6 +171,8 @@ A processor that returns a `ref` will have it's `detail`, `paths`, `ref` and `fr
         fromCache
         fromCache
       }
       }
     }
     }
+    // or, for single-result tasks:
+    // [task.name]: { detail, paths, ref, fromCache }
   }
   }
 }
 }
 ```
 ```
@@ -247,4 +250,3 @@ opts: {
   logLevel: 'debug'
   logLevel: 'debug'
 }
 }
 ```
 ```
-

+ 28 - 2
src/defaults.js

@@ -62,7 +62,7 @@ export const tasks = [
   },
   },
   [
   [
     {
     {
-      name: "taxonomy",
+      name: "tag-taxonomy",
       stateSelectors: ["resources.markdown"],
       stateSelectors: ["resources.markdown"],
       processor: generateTaxonomy,
       processor: generateTaxonomy,
       expand: false,
       expand: false,
@@ -74,6 +74,18 @@ export const tasks = [
       sortAscending: false,
       sortAscending: false,
       skipCache: true,
       skipCache: true,
     },
     },
+    {
+      name: "blog-latest",
+      stateSelectors: ["resources.markdown"],
+      processor: generateTaxonomy,
+      expand: false,
+      orderBy: "date",
+      properties: [
+        "title", "href", "date", "author",
+      ],
+      sortAscending: false,
+      skipCache: true,
+    },
     {
     {
       name: "includes",
       name: "includes",
       inputFiles: [{ pattern: "includes/*.hbs" }],
       inputFiles: [{ pattern: "includes/*.hbs" }],
@@ -92,7 +104,7 @@ export const tasks = [
     },
     },
     {
     {
       name: "render indexes",
       name: "render indexes",
-      stateSelectors: ["resources.taxonomy.taxonomy.detail"],
+      stateSelectors: ["resources.tag-taxonomy.detail"],
       processor: renderTemplate,
       processor: renderTemplate,
       writeOut: true,
       writeOut: true,
       template: "index",
       template: "index",
@@ -101,6 +113,20 @@ export const tasks = [
       outputDir: "by-tag/",
       outputDir: "by-tag/",
       buildFilePath: true,
       buildFilePath: true,
     },
     },
+    {
+      name: "render blog home",
+      stateSelectors: ["resources.blog-latest.detail"],
+      processor: renderTemplate,
+      writeOut: true,
+      template: "index",
+      templateDirs: ["templates/", "~/.rhedyn/templates/"],
+      outputFileExtension: ".html",
+      outputDir: "blog/",
+      expand: false,
+      outputFileName: "index",
+      title: "Blog",
+      buildFilePath: true,
+    },
   ],
   ],
 ]
 ]
 
 

+ 45 - 7
src/lib.js

@@ -242,13 +242,40 @@ export async function expandAndRunTask(meta, config) {
 
 
   if (config.stateSelectors) {
   if (config.stateSelectors) {
     if (config.expand === false) {
     if (config.expand === false) {
-      const inputs = selectState(config.stateSelectors, meta).map(
-        stateItem => stateItem.value.detail,
+      const pathsToStrip = (config.stripPaths || []).map(path =>
+        expandTilde(path),
       )
       )
+      const inputs = selectState(config.stateSelectors, meta).map(stateItem => {
+        return stateItem.value.detail
+          ? stateItem.value.detail
+          : stateItem.value
+      })
+      const decorations = {
+        ...(config.buildFilePath
+          ? {
+            fileOutputPath: path.join(
+              meta.opts.outDir,
+              config.outputDir,
+              replaceFileExtension(
+                removeBasePaths(
+                  pathsToStrip,
+                  config.outputFileName || config.name,
+                ),
+                config.outputFileExtension,
+              ),
+            ),
+          }
+          : {}),
+      }
+      const jobConfig = {
+        ...config,
+        ...decorations,
+        inputs,
+      }
       const jobId = config.jobId || config.name
       const jobId = config.jobId || config.name
       const taskResult = await runTask({
       const taskResult = await runTask({
         meta,
         meta,
-        config: { ...config, inputs },
+        config: { ...jobConfig },
         jobId,
         jobId,
       })
       })
       return [taskResult]
       return [taskResult]
@@ -268,10 +295,21 @@ export async function processTask(meta, task) {
   const taskResult = await expandAndRunTask(meta, task)
   const taskResult = await expandAndRunTask(meta, task)
   const cached = taskResult.filter(taskResult => taskResult.fromCache)
   const cached = taskResult.filter(taskResult => taskResult.fromCache)
   const processed = taskResult.filter(taskResult => !taskResult.fromCache)
   const processed = taskResult.filter(taskResult => !taskResult.fromCache)
-  const resources = taskResult.reduce(
-    (obj, tResult) => (tResult.ref ? { ...obj, [tResult.ref]: tResult } : obj),
-    {},
-  )
+  const resourcesWithRef = taskResult.filter(tResult => tResult.ref)
+  const hasExpandableInputs =
+    (task.inputFiles?.length || task.stateSelectors?.length) &&
+    task.expand !== false
+  const shouldCollapseResources =
+    resourcesWithRef.length === 1 &&
+    taskResult.length === 1 &&
+    !hasExpandableInputs
+  const resources = shouldCollapseResources
+    ? resourcesWithRef[0]
+    : resourcesWithRef.reduce(
+      (obj, tResult) =>
+        tResult.ref ? { ...obj, [tResult.ref]: tResult } : obj,
+      {},
+    )
   const endTime = performance.now()
   const endTime = performance.now()
   const timeTaken = endTime - startTime
   const timeTaken = endTime - startTime
   const hrTime =
   const hrTime =

+ 27 - 17
src/processors.js

@@ -349,23 +349,33 @@ export async function generateTaxonomy({ config }) {
   const sortedInputs = config.sortAscending
   const sortedInputs = config.sortAscending
     ? _.sortBy(config.inputs, orderBy)
     ? _.sortBy(config.inputs, orderBy)
     : _.sortBy(config.inputs, orderBy).reverse()
     : _.sortBy(config.inputs, orderBy).reverse()
-  const taxonomy = allValues.values().reduce((groups, currentGroup) => {
-    const grouped = {
-      ...groups,
-      [currentGroup]: sortedInputs
-        .filter(item => item[config.indexOn].includes(currentGroup))
-        .map(item => {
-          const entry = config.properties
-            ? config.properties.reduce(
-              (ent, prop) => ({ ...ent, [prop]: item[prop] }),
-              {},
-            )
-            : item
-          return entry
-        }),
-    }
-    return grouped
-  }, {})
+  const taxonomy = config.indexOn
+    ? allValues.values().reduce((groups, currentGroup) => {
+      const grouped = {
+        ...groups,
+        [currentGroup]: sortedInputs
+          .filter(item => item[config.indexOn].includes(currentGroup))
+          .map(item => {
+            const entry = config.properties
+              ? config.properties.reduce(
+                (ent, prop) => ({ ...ent, [prop]: item[prop] }),
+                {},
+              )
+              : item
+            return entry
+          }),
+      }
+      return grouped
+    }, {})
+    : sortedInputs.map(item => {
+      const entry = config.properties
+        ? config.properties.reduce(
+          (ent, prop) => ({ ...ent, [prop]: item[prop] }),
+          {},
+        )
+        : item
+      return entry
+    })
   return {
   return {
     detail: taxonomy,
     detail: taxonomy,
     ref: config.name,
     ref: config.name,