Skip to content
This repository has been archived by the owner on Sep 7, 2020. It is now read-only.

Commit

Permalink
Plugin-collector-files: Fix date parse from filename when using sub f…
Browse files Browse the repository at this point in the history
…olders (#1201)
  • Loading branch information
emilpalsson authored and MoOx committed Nov 14, 2017
1 parent 0e15e24 commit 6a593da
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
10 changes: 9 additions & 1 deletion packages/plugin-collector-files/src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
getAuthors,
getFields,
getFieldValue,
injectData
injectData,
parsePath
} from "..";

it("should be able to generate keys", () => {
Expand Down Expand Up @@ -94,3 +95,10 @@ it("should be able to inject date from filename in data", () => {
}
});
});

it("should be able to parse filepath", () => {
expect(parsePath("posts/november/2017-11-11-test.md")).toEqual({
filename: "2017-11-11-test.md",
allPaths: ["posts", "posts/november", "posts/november/2017-11-11-test.md"]
});
});
18 changes: 12 additions & 6 deletions packages/plugin-collector-files/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,24 @@ export function injectData(
};
}

export function parsePath(name: string) {
const pathSegments = name.split(sep);
const allPaths = pathSegments.reduce((acc, v) => {
acc.push(acc.length > 0 ? acc[acc.length - 1] + sep + v : v);
return acc;
}, []);
const filename = pathSegments[pathSegments.length - 1];
return { filename, allPaths };
}

export default function() {
return {
name: "@phenomic/plugin-collector-files",
collect(db: PhenomicDB, name: string, json: PhenomicTransformResult) {
name = normalizeWindowsPath(name);
const key = getKey(name, json);
const adjustedJSON = injectData(name, json);
const pathSegments = name.split(sep);
const allPaths = pathSegments.reduce((acc, v) => {
acc.push(acc.length > 0 ? acc[acc.length - 1] + sep + v : v);
return acc;
}, []);
const { filename, allPaths } = parsePath(name);
const adjustedJSON = injectData(filename, json);
return Promise.all(
allPaths.map(pathName => {
const relativeKey = key.replace(pathName + sep, "");
Expand Down

0 comments on commit 6a593da

Please sign in to comment.