Permalink
Browse files

Plugin-collector-files: Fix date parse from filename when using sub f…

…olders (#1201)
  • Loading branch information...
emilpalsson authored and MoOx committed Nov 14, 2017
1 parent 0e15e24 commit 6a593daf159cc325c48d11efa10a9e19267b1bf6
@@ -4,7 +4,8 @@ import {
getAuthors,
getFields,
getFieldValue,
injectData
injectData,
parsePath
} from "..";
it("should be able to generate keys", () => {
@@ -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"]
});
});
@@ -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, "");

0 comments on commit 6a593da

Please sign in to comment.