Skip to content

Commit

Permalink
bump to v0.3.0 fix cached bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
willin committed Jul 23, 2020
1 parent 0837223 commit 26ce165
Show file tree
Hide file tree
Showing 8 changed files with 1,340 additions and 859 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
root: true
extends: dwing
extends: willin
parser: babel-eslint
rules:
max-len:
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": "nuxtpress",
"version": "0.2.1",
"version": "0.3.0",
"description": "NuxtPress is a professional blog module for nuxt.js",
"module": "./src/index.js",
"jsnext:main": "./src/index.js",
Expand Down
4 changes: 2 additions & 2 deletions src/db/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ export const wordcount = (str) => {
return cn + en;
};

export const parseFrontmatter = content => matter(content, {
export const parseFrontmatter = (content) => matter(content, {
excerpt_separator: '<!-- more -->'
});

export const objectKeysToLower = origin => Object.keys(origin).reduce((obj, key) => {
export const objectKeysToLower = (origin) => Object.keys(origin).reduce((obj, key) => {
Object.assign(obj, { [key.toLocaleLowerCase()]: origin[key] });
return obj;
}, {});
8 changes: 4 additions & 4 deletions src/db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { wordcount, parseFrontmatter, objectKeysToLower } from './common';
import md from './markdown';

export default async (sourceDir) => {
const postFiles = await globby(['**/*.md'], { cwd: sourceDir }).then(arr => arr.sort((x, y) => (x < y ? 1 : -1)));
const postFiles = await globby(['**/*.md'], { cwd: sourceDir }).then((arr) => arr.sort((x, y) => (x < y ? 1 : -1)));

return Promise.all(postFiles.map(async (file) => {
const filepath = path.resolve(sourceDir, file);
Expand All @@ -28,11 +28,11 @@ export default async (sourceDir) => {
date,
slug
};
})).then(posts => ({
})).then((posts) => ({
posts,
...posts.reduce((total, post) => {
post.tags.forEach((tag) => {
const i = total.tags.findIndex(x => x.name === tag);
const i = total.tags.findIndex((x) => x.name === tag);
if (i === -1) {
total.tags.push({ name: tag, count: 1 });
} else {
Expand All @@ -42,7 +42,7 @@ export default async (sourceDir) => {
}
});
post.category.forEach((cat) => {
const i = total.categories.findIndex(x => x.name === cat);
const i = total.categories.findIndex((x) => x.name === cat);
if (i === -1) {
total.categories.push({ name: cat, count: 1 });
} else {
Expand Down
12 changes: 4 additions & 8 deletions src/db/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@ const slugify = require('./slugify');
const md = MD({
html: true
})
.use(anchor, Object.assign({
slugify,
.use(anchor, { slugify,
permalink: true,
permalinkBefore: true,
permalinkSymbol: '#'
}))
.use(toc, Object.assign({
slugify,
includeLevel: [2, 3]
}));
permalinkSymbol: '#' })
.use(toc, { slugify,
includeLevel: [2, 3] });

module.exports = md;
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ export default function () {
const { posts, tags, categories } = await db(src);
const pages = Math.ceil(posts.length / perPage);
[...new Array(pages - 1).fill(pages).map((x, i) => `/page/${x - i}`),
...tags.map(tag => `/tags/${tag.name}`),
...categories.map(category => `/categories/${category.name}`),
...posts.map(post => `/p/${post.slug}`)
...tags.map((tag) => `/tags/${tag.name}`),
...categories.map((category) => `/categories/${category.name}`),
...posts.map((post) => `/p/${post.slug}`)
].forEach((route) => {
routes.push({ route, payload: null });
});
Expand Down
18 changes: 9 additions & 9 deletions src/plugins/np.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@ export default ({ hotReload, route, app, isStatic }, inject) => {
const isAPI = process.server || !isStatic;

const fetchContent = async (endpoint, search = '') => {
const key = endpoint.replace(/(?!^\/)(\/)/g, '.');
const cachedKey = `${key}${search}`;
if (isAPI) {
const key = endpoint.replace(/(?!^\/)(\/)/g, '.');
if (!isStatic || !cache[key]) {
cache[key] = (await app.$axios.get(`/api/${key}${search ? `/${search}` : ''}`)).data;
if (!isStatic || !cache[cachedKey]) {
cache[cachedKey] = (await app.$axios.get(`/api/${key}${search ? `/${search}` : ''}`)).data;
}
return cache[key];
return cache[cachedKey];
}
if (process.client) {
// eslint-disable-next-line no-param-reassign
app.$axios.defaults.baseURL = '/';
const key = endpoint.replace(/(?!^\/)(\/)/g, '.');
if (!cache[key]) {
cache[key] = (await app.$axios.get(`/_nuxt/api/${key}${search ? `_${search}` : ''}.json`)).data;
if (!cache[cachedKey]) {
cache[cachedKey] = (await app.$axios.get(`/_nuxt/api/${key}${search ? `_${search}` : ''}.json`)).data;
}
return cache[key];
return cache[cachedKey];
}
return {};
};

const handler = new Proxy({}, {
get: (target, property) => search => fetchContent(property.toLowerCase(), search)
get: (target, property) => (search) => fetchContent(property.toLowerCase(), search)
});
// short for nuxtpress
inject('np', handler);
Expand Down
Loading

0 comments on commit 26ce165

Please sign in to comment.