Skip to content

Commit

Permalink
feat: add $withDemoBase() method for blog
Browse files Browse the repository at this point in the history
  • Loading branch information
zyao89 committed May 18, 2020
1 parent 689a796 commit 1884923
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 7 deletions.
4 changes: 4 additions & 0 deletions docs/__test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@ bcbbds
sdsdsdsd
sdf
sdf

<!-- $withDemoBase -->

<a :href="$withDemoBase('/abc')" v-text="$withDemoBase('/abc')"></a>
5 changes: 4 additions & 1 deletion theme/helpers/postData.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// 过滤博客数据
export function filterPosts(posts, isTimeline) {
return posts.filter(item => {
const { title, frontmatter: { home, date, private: isPrivate } } = item;
const { title, frontmatter: { home, date, private: isPrivate, __type__ } } = item;
if (__type__ !== 'post') { // 判断是不是 post
return false;
}
return isTimeline === true
? !(home === true || title === undefined || date === undefined || isPrivate === true)
: !(home === true || title === undefined || isPrivate === true);
Expand Down
9 changes: 9 additions & 0 deletions theme/mixins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,13 @@ export default function({ Vue }) {
};

Vue.mixin(mixins);

Vue.prototype.$withDemoBase = function(path) {
const url = this.$themeConfig.siteUrl || this.$site.siteUrl || '';
path = this.$withBase(path);
if (path.charAt(0) !== '/') {
return path;
}
return url.replace(/\/*$/, '') + path;
};
}
13 changes: 7 additions & 6 deletions theme/plugins/blog/extendPageData.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ module.exports = function extendPageData($page, ctx) {
const blogConfig = themeConfig.blogConfig;
if ($page.path.startsWith(ensureBothSlash(blogConfig.postsDir))) {
$page.frontmatter.permalink = $page.frontmatter.permalink || blogConfig.permalink;
if ($page.frontmatter.date) {
const $lang = $page.frontmatter.lang || $page._computed.$localeConfig.lang || lang;
$page.frontmatter.dateFormat = moment($page.frontmatter.date)
.utc().locale($lang)
.format('llll');
}
$page.frontmatter.__type__ = 'post';
}
if ($page.frontmatter.date) {
const $lang = $page.frontmatter.lang || $page._computed.$localeConfig.lang || lang;
$page.frontmatter.dateFormat = moment($page.frontmatter.date)
.utc().locale($lang)
.format('llll');
}
};
3 changes: 3 additions & 0 deletions theme/plugins/blog/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ module.exports = function initBlogConfig(ctx) {
blogConfig.permalink = blogConfig.permalink || '/posts/:year/:month/:day/:slug.html';
blogConfig.rss = blogConfig.rss || false;
blogConfig.copyright = _.isUndefined(blogConfig.copyright) ? true : blogConfig.copyright !== false ? blogConfig.copyright : false;
blogConfig.robots = blogConfig.robots || false;
blogConfig.sitemap = blogConfig.sitemap || false;
blogConfig.comment = blogConfig.comment || false;
return blogConfig;
};

0 comments on commit 1884923

Please sign in to comment.