mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-13 00:03:57 +02:00
fix(v2): slugify tags
This commit is contained in:
parent
fd270bdceb
commit
ba6724281e
4 changed files with 40 additions and 32 deletions
|
@ -12,7 +12,8 @@
|
||||||
"@docusaurus/utils": "^2.0.0-alpha.13",
|
"@docusaurus/utils": "^2.0.0-alpha.13",
|
||||||
"fs-extra": "^7.0.1",
|
"fs-extra": "^7.0.1",
|
||||||
"globby": "^9.1.0",
|
"globby": "^9.1.0",
|
||||||
"loader-utils": "^1.2.3"
|
"loader-utils": "^1.2.3",
|
||||||
|
"lodash": "^4.17.11"
|
||||||
},
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
"@docusaurus/core": "^2.0.0",
|
"@docusaurus/core": "^2.0.0",
|
||||||
|
|
|
@ -5,9 +5,10 @@
|
||||||
* LICENSE file in the root directory of this source tree.
|
* LICENSE file in the root directory of this source tree.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const globby = require('globby');
|
|
||||||
const path = require('path');
|
|
||||||
const fs = require('fs-extra');
|
const fs = require('fs-extra');
|
||||||
|
const globby = require('globby');
|
||||||
|
const _ = require('lodash');
|
||||||
|
const path = require('path');
|
||||||
const {parse, normalizeUrl, docuHash} = require('@docusaurus/utils');
|
const {parse, normalizeUrl, docuHash} = require('@docusaurus/utils');
|
||||||
|
|
||||||
// TODO: Use a better slugify function that doesn't rely on a specific file extension.
|
// TODO: Use a better slugify function that doesn't rely on a specific file extension.
|
||||||
|
@ -137,11 +138,15 @@ class DocusaurusPluginContentBlog {
|
||||||
}
|
}
|
||||||
|
|
||||||
tags.forEach(tag => {
|
tags.forEach(tag => {
|
||||||
const normalizedTag = tag.toLowerCase();
|
const normalizedTag = _.kebabCase(tag);
|
||||||
if (!blogTags[normalizedTag]) {
|
if (!blogTags[normalizedTag]) {
|
||||||
blogTags[normalizedTag] = [];
|
blogTags[normalizedTag] = {
|
||||||
|
name: tag.toLowerCase(), // Will only use the name of the first occurrence of the tag.
|
||||||
|
items: [],
|
||||||
|
};
|
||||||
}
|
}
|
||||||
blogTags[normalizedTag].push(blogPost.id);
|
|
||||||
|
blogTags[normalizedTag].items.push(blogPost.id);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -253,21 +258,18 @@ class DocusaurusPluginContentBlog {
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
Object.keys(blogTags).map(async tag => {
|
Object.keys(blogTags).map(async tag => {
|
||||||
const permalink = normalizeUrl([tagsPath, tag]);
|
const permalink = normalizeUrl([tagsPath, tag]);
|
||||||
const postIDs = blogTags[tag];
|
const {name, items} = blogTags[tag];
|
||||||
|
|
||||||
tagsModule[tag] = {
|
tagsModule[tag] = {
|
||||||
count: postIDs.length,
|
slug: tag,
|
||||||
|
name,
|
||||||
|
count: items.length,
|
||||||
permalink,
|
permalink,
|
||||||
};
|
};
|
||||||
|
|
||||||
const tagsMetadataPath = await createData(
|
const tagsMetadataPath = await createData(
|
||||||
`${docuHash(permalink)}.json`,
|
`${docuHash(permalink)}.json`,
|
||||||
JSON.stringify(
|
JSON.stringify(tagsModule[tag], null, 2),
|
||||||
{
|
|
||||||
tag,
|
|
||||||
},
|
|
||||||
null,
|
|
||||||
2,
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
addRoute({
|
addRoute({
|
||||||
|
@ -275,7 +277,7 @@ class DocusaurusPluginContentBlog {
|
||||||
component: blogTagsPostsComponent,
|
component: blogTagsPostsComponent,
|
||||||
exact: true,
|
exact: true,
|
||||||
modules: {
|
modules: {
|
||||||
items: postIDs.map(postID => {
|
items: items.map(postID => {
|
||||||
const {metadata: postMetadata, metadataPath} = blogItemsToModules[
|
const {metadata: postMetadata, metadataPath} = blogItemsToModules[
|
||||||
postID
|
postID
|
||||||
];
|
];
|
||||||
|
@ -296,6 +298,8 @@ class DocusaurusPluginContentBlog {
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Only create /tags page if there are tags.
|
||||||
|
if (Object.keys(blogTags).length > 0) {
|
||||||
const tagsListPath = await createData(
|
const tagsListPath = await createData(
|
||||||
`${docuHash(`${tagsPath}-tags`)}.json`,
|
`${docuHash(`${tagsPath}-tags`)}.json`,
|
||||||
JSON.stringify(tagsModule, null, 2),
|
JSON.stringify(tagsModule, null, 2),
|
||||||
|
@ -310,6 +314,7 @@ class DocusaurusPluginContentBlog {
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
getThemePath() {
|
getThemePath() {
|
||||||
return path.resolve(__dirname, './theme');
|
return path.resolve(__dirname, './theme');
|
||||||
|
|
|
@ -43,7 +43,7 @@ function BlogTagsListPage(props) {
|
||||||
className="padding-right--md"
|
className="padding-right--md"
|
||||||
href={tags[tag].permalink}
|
href={tags[tag].permalink}
|
||||||
key="tag">
|
key="tag">
|
||||||
{tag} ({tags[tag].count})
|
{tags[tag].name} ({tags[tag].count})
|
||||||
</Link>
|
</Link>
|
||||||
))}
|
))}
|
||||||
<hr />
|
<hr />
|
||||||
|
|
|
@ -12,15 +12,17 @@ import BlogPostItem from '@theme/BlogPostItem';
|
||||||
|
|
||||||
function BlogTagsPostPage(props) {
|
function BlogTagsPostPage(props) {
|
||||||
const {metadata, items} = props;
|
const {metadata, items} = props;
|
||||||
const {tag} = metadata;
|
const {name: tagName, count} = metadata;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Layout title={`Blog | Tagged ${tag}`} description={`Blog | Tagged ${tag}`}>
|
<Layout
|
||||||
|
title={`Blog | Tagged "${tagName}"`}
|
||||||
|
description={`Blog | Tagged "${tagName}"`}>
|
||||||
<div className="container margin-vert--xl">
|
<div className="container margin-vert--xl">
|
||||||
<div className="row">
|
<div className="row">
|
||||||
<div className="col col--8 col--offset-2">
|
<div className="col col--8 col--offset-2">
|
||||||
<h1>
|
<h1>
|
||||||
{items.length} post(s) tagged with "{tag}"
|
{count} post(s) tagged with "{tagName}"
|
||||||
</h1>
|
</h1>
|
||||||
<div className="margin-vert--lg">
|
<div className="margin-vert--lg">
|
||||||
{items.map(
|
{items.map(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue