Skip to content

Commit

Permalink
feat(plugin-sitemap): added public files to sitemap
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanMatthias committed Jan 14, 2019
1 parent aa61f30 commit 5a6bceb
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 882 deletions.
4 changes: 4 additions & 0 deletions packages/plugin-sitemap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
},
"dependencies": {
"@origami/core": "0.0.3-alpha.11",
"recursive-readdir": "^2.2.2",
"sitemap": "^2.1.0"
},
"devDependencies": {
"@types/recursive-readdir": "^2.2.0"
}
}
3 changes: 2 additions & 1 deletion packages/plugin-sitemap/src/initialItems.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { config } from '@origami/core';
import { SitemapItem } from '.';
import { appPages } from './integrations/appPages';
import { publicFiles } from './integrations/publicFiles';

const integrations = [appPages];
const integrations = [appPages, publicFiles];

export const initialItems = async (sm: any): Promise<SitemapItem[]> => {
let items: SitemapItem[] = [];
Expand Down
46 changes: 46 additions & 0 deletions packages/plugin-sitemap/src/integrations/publicFiles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Origami } from '@origami/core';
import path from 'path';
// tslint:disable-next-line match-default-export-name
import readdir from 'recursive-readdir';
import { SitemapItem } from '..';

/**
* Publicly hosted directories
* Load all html pages from public locations recursively
*/
export const publicFiles = async (
config: Origami.Config
): Promise<SitemapItem[] | false> => {
let staticDir;
if (config.server && config.server.static) staticDir = config.server.static;

if (!staticDir || staticDir === true) staticDir = 'public';
if (!(staticDir instanceof Array)) staticDir = [staticDir];

let items: SitemapItem[] = [];

await Promise.all(staticDir.map(async (d) => {
const dir = path.resolve(process.cwd(), d);

items = items.concat(await Promise.all(
(await readdir(dir))
// Remove non html files
.filter((f) => f.endsWith('.html'))
// Convert to SitemapItem
.map(async (f) => {
// Remove HTML extension
let url = path.relative(dir, f).slice(0, '.html'.length * -1);
// Remove the /index
if (url.endsWith('index')) url = url.slice(0, 'index'.length * -1);

return {
url: `/${url}`,
changeFreq: 'daily',
lastmodfile: f
} as SitemapItem;
})
));
}));

return items;
};
Loading

0 comments on commit 5a6bceb

Please sign in to comment.