fix(v2): hide read more button on non-truncated posts (#2240)

* fix(v2): hide read more button on non-truncated posts

* Update tests
This commit is contained in:
Alexey Pyltsyn 2020-01-24 08:31:07 +03:00 committed by Yangshun Tay
parent 894dbcff49
commit 5121ac013c
6 changed files with 12 additions and 7 deletions

View file

@ -57,6 +57,7 @@ describe('loadBlog', () => {
permalink: noDatePermalink, permalink: noDatePermalink,
title: 'no date', title: 'no date',
}, },
truncated: false,
}); });
expect( expect(
blogPosts.find(v => v.metadata.title === 'Happy 1st Birthday Slash!') blogPosts.find(v => v.metadata.title === 'Happy 1st Birthday Slash!')
@ -76,6 +77,7 @@ describe('loadBlog', () => {
permalink: '/blog/2019/01/01/date-matter', permalink: '/blog/2019/01/01/date-matter',
title: 'date-matter', title: 'date-matter',
}, },
truncated: false,
}); });
expect( expect(
@ -91,6 +93,7 @@ describe('loadBlog', () => {
permalink: '/blog/2019/01/01/date-matter', permalink: '/blog/2019/01/01/date-matter',
title: 'date-matter', title: 'date-matter',
}, },
truncated: false,
}); });
}); });
}); });

View file

@ -13,7 +13,7 @@ import {PluginOptions, BlogPost, DateLink} from './types';
import {parse, normalizeUrl, aliasedSitePath} from '@docusaurus/utils'; import {parse, normalizeUrl, aliasedSitePath} from '@docusaurus/utils';
import {LoadContext} from '@docusaurus/types'; import {LoadContext} from '@docusaurus/types';
export function truncate(fileString: string, truncateMarker: RegExp | string) { export function truncate(fileString: string, truncateMarker: RegExp) {
return fileString.split(truncateMarker, 1).shift()!; return fileString.split(truncateMarker, 1).shift()!;
} }
@ -85,7 +85,7 @@ export async function generateBlogPosts(
{siteConfig, siteDir}: LoadContext, {siteConfig, siteDir}: LoadContext,
options: PluginOptions, options: PluginOptions,
) { ) {
const {include, routeBasePath} = options; const {include, routeBasePath, truncateMarker} = options;
if (!fs.existsSync(blogDir)) { if (!fs.existsSync(blogDir)) {
return null; return null;
@ -105,7 +105,7 @@ export async function generateBlogPosts(
const blogFileName = path.basename(relativeSource); const blogFileName = path.basename(relativeSource);
const fileString = await fs.readFile(source, 'utf-8'); const fileString = await fs.readFile(source, 'utf-8');
const {frontMatter, excerpt} = parse(fileString); const {frontMatter, content, excerpt} = parse(fileString);
let date; let date;
// Extract date and title from filename. // Extract date and title from filename.
@ -137,6 +137,7 @@ export async function generateBlogPosts(
date, date,
tags: frontMatter.tags, tags: frontMatter.tags,
title: frontMatter.title, title: frontMatter.title,
truncated: truncateMarker?.test(content) || false,
}, },
}); });
}), }),

View file

@ -41,7 +41,7 @@ const DEFAULT_OPTIONS: PluginOptions = {
blogTagsPostsComponent: '@theme/BlogTagsPostsPage', blogTagsPostsComponent: '@theme/BlogTagsPostsPage',
remarkPlugins: [], remarkPlugins: [],
rehypePlugins: [], rehypePlugins: [],
truncateMarker: /<!--\s*(truncate)\s*-->/, // string or regex truncateMarker: /<!--\s*(truncate)\s*-->/, // Regex
}; };
function assertFeedTypes(val: any): asserts val is FeedType { function assertFeedTypes(val: any): asserts val is FeedType {

View file

@ -12,7 +12,7 @@ import {truncate} from './blogUtils';
export = function(fileString: string) { export = function(fileString: string) {
const callback = this.async(); const callback = this.async();
const {truncateMarker}: {truncateMarker: RegExp | string} = getOptions(this); const {truncateMarker}: {truncateMarker: RegExp} = getOptions(this);
let finalContent = fileString; let finalContent = fileString;

View file

@ -30,7 +30,7 @@ export interface PluginOptions {
blogTagsPostsComponent: string; blogTagsPostsComponent: string;
remarkPlugins: string[]; remarkPlugins: string[];
rehypePlugins: string[]; rehypePlugins: string[];
truncateMarker: RegExp | string; truncateMarker: RegExp;
feedOptions?: { feedOptions?: {
type: FeedType; type: FeedType;
title?: string; title?: string;
@ -79,6 +79,7 @@ export interface MetaData {
title: string; title: string;
prevItem?: Paginator; prevItem?: Paginator;
nextItem?: Paginator; nextItem?: Paginator;
truncated: boolean;
} }
export interface Paginator { export interface Paginator {

View file

@ -24,7 +24,7 @@ function BlogListPage(props) {
key={BlogPostContent.metadata.permalink} key={BlogPostContent.metadata.permalink}
frontMatter={BlogPostContent.frontMatter} frontMatter={BlogPostContent.frontMatter}
metadata={BlogPostContent.metadata} metadata={BlogPostContent.metadata}
truncated> truncated={BlogPostContent.metadata.truncated}>
<BlogPostContent /> <BlogPostContent />
</BlogPostItem> </BlogPostItem>
))} ))}