-
Notifications
You must be signed in to change notification settings - Fork 257
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(routeinfo): pulled complete roueInfo insto route-discovery phase (…
- Loading branch information
1 parent
be48c03
commit b587309
Showing
16 changed files
with
134 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 21 additions & 31 deletions
52
scully/renderPlugins/content-render-utils/readFileAndCheckPrePublishSlug.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,42 @@ | ||
import {readFileSync, writeFileSync} from 'fs'; | ||
import {stringify} from 'yamljs'; | ||
import {HandledRoute} from '../../routerPlugins/addOptionalRoutesPlugin'; | ||
import {randomString} from '../../utils/randomString'; | ||
const fm = require('front-matter'); | ||
|
||
export async function readFileAndCheckPrePublishSlug(file, route: HandledRoute) { | ||
export interface ContentMetaData { | ||
author?: string; | ||
published?: boolean; | ||
slug?: string; | ||
'publish date'?: Date; | ||
slugs?: string[]; | ||
title?: string; | ||
[key: string]: any; | ||
} | ||
|
||
export async function readFileAndCheckPrePublishSlug(file) { | ||
const prependString = '___UNPUBLISHED___'; | ||
const createSlug = () => `${prependString}${Date.now().toString(36)}_${randomString(32)}`; | ||
const originalFile = readFileSync(file, 'utf-8'); | ||
const {attributes: meta, body: fileContent} = fm(originalFile); | ||
const {attributes: meta, body: fileContent}: {attributes: ContentMetaData; body: string} = fm(originalFile); | ||
let prePublished = false; | ||
if (meta.hasOwnProperty('published') && meta.published === false) { | ||
/** this post needs an pre-publish slug */ | ||
const slugs = Array.isArray(meta.slugs) ? meta.slugs : []; | ||
let slug = slugs.find((sl: string) => sl.startsWith(prependString)); | ||
if (!slug) { | ||
let unPublishedSlug = slugs.find((sl: string) => sl.startsWith(prependString)); | ||
if (!unPublishedSlug) { | ||
/** there is no prepub slug so create one and update the file */ | ||
slug = createSlug(); | ||
meta.slugs = slugs.concat(slug); | ||
unPublishedSlug = createSlug(); | ||
meta.slugs = slugs.concat(unPublishedSlug); | ||
/** string literal, don't format "correctly" or things will break ;) */ | ||
const newFile = `--- | ||
${stringify(meta)} | ||
--- | ||
${fileContent}`; | ||
writeFileSync(file, newFile); | ||
} | ||
/** replace the route with the prepub slug */ | ||
const updatedRoute = replaceRouteWithSlug(route.route, slug); | ||
route.route = updatedRoute; | ||
} else if (typeof meta.slug === 'string') { | ||
route.route = replaceRouteWithSlug( | ||
// TODO better handling of url/filename escaping | ||
route.route, | ||
encodeURIComponent( | ||
meta.slug | ||
.trim() | ||
.split('/') | ||
.join('_') | ||
.split(' ') | ||
.join('_') | ||
.split('?') | ||
.join('_') | ||
) | ||
); | ||
/** overwrite slug from file with prepub only in memory. we don't want a file with the original slug name now. */ | ||
meta.slug = unPublishedSlug; | ||
prePublished = true; | ||
} | ||
return {meta, fileContent}; | ||
} | ||
|
||
function replaceRouteWithSlug(route: string, slug: string) { | ||
const lastPart = route.split('/').pop(); | ||
return route.replace(lastPart, slug); | ||
return {meta, fileContent, prePublished}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,70 @@ | ||
import {readdir} from 'fs'; | ||
import {basename, extname, join} from 'path'; | ||
import {configValidator, registerPlugin} from '../pluginManagement/pluginRepository'; | ||
import {registerPlugin} from '../pluginManagement/pluginRepository'; | ||
import {readFileAndCheckPrePublishSlug} from '../renderPlugins/content-render-utils/readFileAndCheckPrePublishSlug'; | ||
import {scullyConfig} from '../utils/config'; | ||
import {RouteTypeContentFolder} from '../utils/interfacesandenums'; | ||
import {log, yellow} from '../utils/log'; | ||
import {HandledRoute} from './addOptionalRoutesPlugin'; | ||
|
||
export async function contentFolderPlugin( | ||
route: string, | ||
angularRoute: string, | ||
conf: RouteTypeContentFolder | ||
): Promise<HandledRoute[]> { | ||
const parts = route.split('/'); | ||
const parts = angularRoute.split('/'); | ||
/** for now, just handle the First parameter. Not sure if/how we can handle multiple ones. */ | ||
const param = parts.filter(p => p.startsWith(':')).map(id => id.slice(1))[0]; | ||
const paramConfig = conf[param]; | ||
if (!paramConfig) { | ||
console.error(`missing config for parameters (${param}) in route: ${route}. Skipping`); | ||
console.error(`missing config for parameters (${param}) in route: ${angularRoute}. Skipping`); | ||
return []; | ||
} | ||
const baseRoute = route.split(':' + param)[0]; | ||
const baseRoute = angularRoute.split(':' + param)[0]; | ||
const path = join(scullyConfig.homeFolder, paramConfig.folder); | ||
log(`Finding files in folder "${yellow(path)}"`); | ||
const files = await new Promise<string[]>(resolve => readdir(path, (err, data) => resolve(data))); | ||
return files.map<HandledRoute>(file => { | ||
const ext = extname(file); | ||
const base = basename(file, ext); | ||
return { | ||
route: `${baseRoute}${base}`, | ||
const handledRoutes: HandledRoute[] = []; | ||
for (const sourceFile of files) { | ||
const ext = extname(sourceFile); | ||
const templateFile = join(path, sourceFile); | ||
const base = basename(sourceFile, ext); | ||
const routify = frag => `${baseRoute}${slugify(frag)}`; | ||
const {meta, prePublished} = await readFileAndCheckPrePublishSlug(templateFile); | ||
const handledRoute: HandledRoute = { | ||
route: routify(meta.slug || base), | ||
type: conf.type, | ||
templateFile: join(path, file), | ||
templateFile, | ||
data: {...meta, sourceFile}, | ||
}; | ||
}); | ||
handledRoutes.push(handledRoute); | ||
if (!prePublished && Array.isArray(meta.slugs)) { | ||
/** also add routes for all available slugs */ | ||
meta.slugs | ||
.filter(slug => typeof slug === 'string') | ||
.map(routify) | ||
.forEach(route => handledRoutes.push({...handledRoute, route})); | ||
} | ||
} | ||
return handledRoutes; | ||
} | ||
|
||
export function slugify(frag: string): string { | ||
return encodeURIComponent( | ||
frag | ||
.trim() | ||
.split('/') | ||
.join('_') | ||
.split(' ') | ||
.join('_') | ||
.split('?') | ||
.join('_') | ||
); | ||
} | ||
|
||
// TODO actual validation of the config | ||
contentFolderPlugin[configValidator] = async conf => { | ||
const configValidator = async conf => { | ||
// return [yellow('all seems ok')]; | ||
return []; | ||
}; | ||
|
||
registerPlugin('router', 'contentFolder', contentFolderPlugin); | ||
registerPlugin('router', 'contentFolder', contentFolderPlugin, configValidator); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.