Skip to content
This repository has been archived by the owner on Jul 28, 2019. It is now read-only.

Commit

Permalink
feat(Export): implement static tag pages
Browse files Browse the repository at this point in the history
  • Loading branch information
Emanuel Kluge committed Oct 9, 2017
1 parent 3ade421 commit 5c1f922
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
19 changes: 17 additions & 2 deletions lib/get-path-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { join, basename, dirname } from 'path';
import * as globby from 'globby';
import getConfig from './config';
import { getGlobPatterns } from './util';
import { ExportPathMap } from './definitions/global';
import getData from './get-data';
import { Tags, ExportPathMap } from './definitions/global';

const removeSlashes = (str: string): string => str.replace(/^\/|\/$/g, '');

Expand Down Expand Up @@ -56,6 +57,18 @@ const getPages = (pages: string[]): ExportPathMap =>
{}
);

const getTags = (tags: Tags): ExportPathMap =>
Object.keys(tags).reduce(
(acc: ExportPathMap, tag: string): ExportPathMap => ({
...acc,
[`/tag/${tag}/`]: {
query: { tag },
page: '/tag',
},
}),
{}
);

const getImageQueryFromPath = (image: string) => {
const parts = image.split('/').filter(Boolean);
return {
Expand All @@ -78,6 +91,7 @@ const getImages = (albumsDir: string, images: string[]): ExportPathMap =>

export default async (): Promise<ExportPathMap> => {
const conf = getConfig();
const { tags } = await getData(conf);
const imagesPattern = join('static', conf.albumsDir, '**/*.{jpg,png,gif}');
const patterns = getGlobPatterns(conf);
const albumPaths = await getPaths(
Expand Down Expand Up @@ -105,6 +119,7 @@ export default async (): Promise<ExportPathMap> => {
},
getAlbums(conf.albumsDir, albumPaths),
getPages(pagePathsExceptIndex),
getImages(conf.albumsDir, imagePaths)
getImages(conf.albumsDir, imagePaths),
getTags(tags)
);
};
19 changes: 18 additions & 1 deletion lib/write-api-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import {
Album,
Image,
Page,
Tags,
FrontpageApiData,
ImageApiData,
TagApiData,
} from './definitions/global';

const pWriteFile = promisify(writeFile);
Expand Down Expand Up @@ -93,12 +95,26 @@ const writeAlbumsData = (config: Config) => async (
);
};

const writeTagsData = async (tagFolder: string, tags: Tags): Promise<void[]> =>
Promise.all(
Object.keys(tags).map(async (tag: string): Promise<void> => {
const destination = path.join(tagFolder, `${tag}.json`);
const data = {
title: tag,
images: tags[tag],
};
await writeData<TagApiData>(destination, data);
})
);

export default async (config: Config): Promise<void> => {
const { albums, pages } = await getData(config);
const { albums, pages, tags } = await getData(config);
const albumFolders = albums.map(({ content }: Album) =>
path.join(config.outDir, 'data', config.albumsDir, content.name)
);
const tagFolder = path.join(config.outDir, 'data', 'tag');
const frontpageApiData = await getFrontpageApiData(config);
await pMkdirp(tagFolder);
await Promise.all(albumFolders.map(createFolder));
await Promise.all([
writeFrontpageData(config.outDir, frontpageApiData),
Expand All @@ -108,5 +124,6 @@ export default async (config: Config): Promise<void> => {
.map(writePagesData(config.outDir))
),
Promise.all(albums.map(writeAlbumsData(config))),
writeTagsData(tagFolder, tags),
]);
};

0 comments on commit 5c1f922

Please sign in to comment.