feat: add utils | page
This commit is contained in:
48
utils/getContent.js
Normal file
48
utils/getContent.js
Normal file
@@ -0,0 +1,48 @@
|
||||
export default async ($content, params, error, path) => {
|
||||
const currentPage = parseInt(params.page)
|
||||
/**
|
||||
* Số items trên 1 page
|
||||
*/
|
||||
const perPage = 14
|
||||
const allArticles = await $content(path).fetch()
|
||||
const totalArticles = allArticles.length
|
||||
|
||||
/**
|
||||
* Số trang
|
||||
* Tổng items / items trên 1 page
|
||||
*/
|
||||
const lastPage = Math.ceil(totalArticles / perPage)
|
||||
const lastPageCount = totalArticles % perPage
|
||||
|
||||
/**
|
||||
* Tính số items cần bỏ qua khi get
|
||||
*/
|
||||
const skipNumber = () => {
|
||||
if (currentPage === 1) {
|
||||
return 0
|
||||
}
|
||||
if (currentPage === lastPage) {
|
||||
return totalArticles - lastPageCount
|
||||
}
|
||||
return (currentPage - 1) * perPage
|
||||
}
|
||||
|
||||
/**
|
||||
* get items skip
|
||||
*/
|
||||
const paginatedArticles = await $content(path)
|
||||
.only(['title', 'description', 'image', 'slug', 'tags', 'published'])
|
||||
.sortBy('published', 'desc')
|
||||
.limit(perPage)
|
||||
.skip(skipNumber())
|
||||
.fetch()
|
||||
|
||||
if (currentPage === 0 || !paginatedArticles.length) {
|
||||
return error({ statusCode: 404, message: 'No articles found!' })
|
||||
}
|
||||
|
||||
return {
|
||||
allArticles,
|
||||
paginatedArticles
|
||||
}
|
||||
}
|
6
utils/getRoutes.js
Normal file
6
utils/getRoutes.js
Normal file
@@ -0,0 +1,6 @@
|
||||
export default async () => {
|
||||
const { $content } = require('@nuxt/content');
|
||||
const files = await $content({ deep: true }).only(['path']).fetch();
|
||||
|
||||
return files.map((file) => (file.path === '/index' ? '/' : file.path));
|
||||
};
|
56
utils/getSiteMeta.js
Normal file
56
utils/getSiteMeta.js
Normal file
@@ -0,0 +1,56 @@
|
||||
import global from './global';
|
||||
|
||||
export default (meta) => {
|
||||
return [
|
||||
{
|
||||
hid: 'description',
|
||||
name: 'description',
|
||||
content: (meta && meta.description) || global.siteDesc,
|
||||
},
|
||||
{
|
||||
hid: 'og:type',
|
||||
property: 'og:type',
|
||||
content: (meta && meta.type) || global.siteType,
|
||||
},
|
||||
{
|
||||
hid: 'og:url',
|
||||
property: 'og:url',
|
||||
content: (meta && meta.url) || global.siteUrl,
|
||||
},
|
||||
{
|
||||
hid: 'og:title',
|
||||
property: 'og:title',
|
||||
content: (meta && meta.title) || global.siteTitle,
|
||||
},
|
||||
{
|
||||
hid: 'og:description',
|
||||
property: 'og:description',
|
||||
content: (meta && meta.description) || global.siteDesc,
|
||||
},
|
||||
{
|
||||
hid: 'og:image',
|
||||
property: 'og:image',
|
||||
content: (meta && meta.mainImage) || global.mainImage,
|
||||
},
|
||||
{
|
||||
hid: 'twitter:url',
|
||||
name: 'twitter:url',
|
||||
content: (meta && meta.url) || global.siteUrl,
|
||||
},
|
||||
{
|
||||
hid: 'twitter:title',
|
||||
name: 'twitter:title',
|
||||
content: (meta && meta.title) || global.siteTitle,
|
||||
},
|
||||
{
|
||||
hid: 'twitter:description',
|
||||
name: 'twitter:description',
|
||||
content: (meta && meta.description) || global.siteDesc,
|
||||
},
|
||||
{
|
||||
hid: 'twitter:image',
|
||||
name: 'twitter:image',
|
||||
content: (meta && meta.mainImage) || global.mainImage,
|
||||
},
|
||||
];
|
||||
};
|
18
utils/global.js
Normal file
18
utils/global.js
Normal file
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
these are the global variables, make sure to change them for yours
|
||||
you can add anything you like here, just import this file and
|
||||
access any variable via object syntax global.yourVarName
|
||||
*/
|
||||
export default {
|
||||
siteUrl: 'https://example.com',
|
||||
siteName: 'Starter Blog',
|
||||
author: 'Joe Blogs',
|
||||
twitterHandle: '@joeblogs',
|
||||
twitterURL: 'https://twitter.com/garethredfern',
|
||||
githubURL: 'https://github.com/garethredfern',
|
||||
siteTitle: 'Add Your Main Site Title Here',
|
||||
siteDesc:
|
||||
'A description for your site here, this will show on the home page.',
|
||||
mainImage: '',
|
||||
siteType: 'website',
|
||||
};
|
Reference in New Issue
Block a user