Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Jan 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ describe('loadBlog', () => {
permalink: noDatePermalink,
title: 'no date',
},
truncated: false,
});
expect(
blogPosts.find(v => v.metadata.title === 'Happy 1st Birthday Slash!')
Expand All @@ -76,6 +77,7 @@ describe('loadBlog', () => {
permalink: '/blog/2019/01/01/date-matter',
title: 'date-matter',
},
truncated: false,
});

expect(
Expand All @@ -91,6 +93,7 @@ describe('loadBlog', () => {
permalink: '/blog/2019/01/01/date-matter',
title: 'date-matter',
},
truncated: false,
});
});
});
7 changes: 4 additions & 3 deletions packages/docusaurus-plugin-content-blog/src/blogUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()!;
}

Expand Down Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -137,6 +137,7 @@ export async function generateBlogPosts(
date,
tags: frontMatter.tags,
title: frontMatter.title,
truncated: truncateMarker?.test(content) || false,
},
});
}),
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus-plugin-content-blog/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
3 changes: 2 additions & 1 deletion packages/docusaurus-plugin-content-blog/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export interface PluginOptions {
blogTagsPostsComponent: string;
remarkPlugins: string[];
rehypePlugins: string[];
truncateMarker: RegExp | string;
truncateMarker: RegExp;
feedOptions?: {
type: FeedType;
title?: string;
Expand Down Expand Up @@ -79,6 +79,7 @@ export interface MetaData {
title: string;
prevItem?: Paginator;
nextItem?: Paginator;
truncated: boolean;
}

export interface Paginator {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function BlogListPage(props) {
key={BlogPostContent.metadata.permalink}
frontMatter={BlogPostContent.frontMatter}
metadata={BlogPostContent.metadata}
truncated>
truncated={BlogPostContent.metadata.truncated}>
<BlogPostContent />
</BlogPostItem>
))}
Expand Down