Skip to content

Commit

Permalink
🚧 Support for tags
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Nov 18, 2019
1 parent 9f600e7 commit e78dd8f
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .staartrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"title": "Staart Site",
"author": "[O15Y](https://o15y.com)"
"author": "[O15Y](https://o15y.com)",
"noDelayWithoutToken": "true"
}
7 changes: 7 additions & 0 deletions content/code.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
---
tags:
- articles
- programming
- city: new-delhi
---

# Code

This page shows and example of how code looks.
Expand Down
5 changes: 5 additions & 0 deletions content/getting-started/guide-2.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
---
tags:
- city: new-york
---

# Guide 2

This is the second guide, to show a demo of breadcrumbs.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@staart/site",
"version": "1.9.1",
"version": "1.9.2",
"module": "dist/module.js",
"main": "dist/index.js",
"bin": "./dist/index.js",
Expand Down
10 changes: 9 additions & 1 deletion src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
} from "./data";
import { minify } from "html-minifier";
import { render as scss } from "sass";
import { removeHeading, getTitle } from "./parse";
import { removeHeading, getTitle, getTags, getFilesForTag } from "./parse";
import { getConfig } from "./config";
import { SitemapStream, streamToPromise } from "sitemap";
import { StaartSiteConfig } from "./interfaces";
Expand Down Expand Up @@ -279,6 +279,14 @@ const generateSitemap = async () => {
)
);
}
const tags = await getTags();
for await (const key of Object.keys(tags)) {
const values = tags[key];
for await (const value of values) {
const files = await getFilesForTag(key, value);
console.log("Files for", key, value, files);
}
}
};

const generatePage = async (path: string, content: string) => {
Expand Down
1 change: 1 addition & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface StaartSiteConfig {
stylePath?: string;
scriptPath?: string;
homePath?: string;
tagsName?: string;
redirectsPath?: string;
hostname?: string;
navbar?: string[];
Expand Down
64 changes: 64 additions & 0 deletions src/parse.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import marked, { Renderer } from "marked";
import frontMatter from "front-matter";
import { FrontMatter } from "./interfaces";
import { listContentFiles, readContentFile } from "./files";
import { getConfig } from "./config";

export const renderMd = (md: string, avoidParagraphs = false) => {
const renderer = new Renderer();
Expand Down Expand Up @@ -29,3 +31,65 @@ export const getTitle = async (md: string, keepEmoji = true) => {
title = `<span aria-hidden="true">${attributes.emoji}</span> ${title}`;
return title;
};

export const getTags = async () => {
const config = await getConfig();
const tagsSlug = config.tagsName || "tags";
const allTags: {
[index: string]: string[];
} = {};
const files = await listContentFiles();
for await (const file of files) {
const attributes = frontMatter(await readContentFile(file)).attributes as {
tags?: (
| string
| {
[index: string]: string;
}
)[];
};
if (attributes.tags)
attributes.tags.forEach(tag => {
if (typeof tag === "object") {
Object.keys(tag).forEach(key => {
allTags[key] = allTags[key] || [];
allTags[key].push(tag[key]);
});
} else {
allTags[tagsSlug] = allTags[tagsSlug] || [];
allTags[tagsSlug].push(tag);
}
});
}
return allTags;
};

export const getFilesForTag = async (key: string, value: string) => {
const files = await listContentFiles();
const config = await getConfig();
const tagsSlug = config.tagsName || "tags";
const result: string[] = [];
for await (const file of files) {
let has = false;
const attributes = frontMatter(await readContentFile(file)).attributes as {
tags?: (
| string
| {
[index: string]: string;
}
)[];
};
if (attributes.tags)
attributes.tags.forEach(tag => {
if (typeof tag === "object") {
Object.keys(tag).forEach(k => {
if (k === key && tag[k] === value) has = true;
});
} else {
if (key === tagsSlug && value === tag) has = true;
}
});
if (has) result.push(file);
}
return result;
};

0 comments on commit e78dd8f

Please sign in to comment.