mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-12 16:47:26 +02:00
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:
parent
894dbcff49
commit
5121ac013c
6 changed files with 12 additions and 7 deletions
|
@ -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,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -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,
|
||||
},
|
||||
});
|
||||
}),
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -24,7 +24,7 @@ function BlogListPage(props) {
|
|||
key={BlogPostContent.metadata.permalink}
|
||||
frontMatter={BlogPostContent.frontMatter}
|
||||
metadata={BlogPostContent.metadata}
|
||||
truncated>
|
||||
truncated={BlogPostContent.metadata.truncated}>
|
||||
<BlogPostContent />
|
||||
</BlogPostItem>
|
||||
))}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue