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,
title: 'no date',
},
truncated: false,
});
expect(
blogPosts.find(v => v.metadata.title === 'Happy 1st Birthday Slash!')
@ -76,6 +77,7 @@ describe('loadBlog', () => {
permalink: '/blog/2019/01/01/date-matter',
title: 'date-matter',
},
truncated: false,
});
expect(
@ -91,6 +93,7 @@ describe('loadBlog', () => {
permalink: '/blog/2019/01/01/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 {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()!;
}
@ -85,7 +85,7 @@ export async function generateBlogPosts(
{siteConfig, siteDir}: LoadContext,
options: PluginOptions,
) {
const {include, routeBasePath} = options;
const {include, routeBasePath, truncateMarker} = options;
if (!fs.existsSync(blogDir)) {
return null;
@ -105,7 +105,7 @@ export async function generateBlogPosts(
const blogFileName = path.basename(relativeSource);
const fileString = await fs.readFile(source, 'utf-8');
const {frontMatter, excerpt} = parse(fileString);
const {frontMatter, content, excerpt} = parse(fileString);
let date;
// Extract date and title from filename.
@ -137,6 +137,7 @@ export async function generateBlogPosts(
date,
tags: frontMatter.tags,
title: frontMatter.title,
truncated: truncateMarker?.test(content) || false,
},
});
}),

View file

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

View file

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

View file

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