mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-10 07:37:19 +02:00
feat(v2): docs plugin initial work (#1327)
* feat(v2): pluginify docs * feat(v2): implement docs plugin * fix(v2): fix bugs in docs plugin for translation and versioning
This commit is contained in:
parent
c33e874e1c
commit
a70d9b6720
32 changed files with 576 additions and 371 deletions
179
packages/docusaurus-plugin-content-docs/src/metadata.js
Normal file
179
packages/docusaurus-plugin-content-docs/src/metadata.js
Normal file
|
@ -0,0 +1,179 @@
|
|||
/**
|
||||
* Copyright (c) 2017-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
const fs = require('fs-extra');
|
||||
const path = require('path');
|
||||
const {getSubFolder, idx, parse, normalizeUrl} = require('@docusaurus/utils');
|
||||
|
||||
function getLanguage(filepath, refDir, env) {
|
||||
const translationEnabled = idx(env, ['translation', 'enabled']);
|
||||
|
||||
if (translationEnabled) {
|
||||
const detectedLangTag = getSubFolder(filepath, refDir);
|
||||
const enabledLanguages = idx(env, ['translation', 'enabledLanguages']);
|
||||
const langTags =
|
||||
(enabledLanguages && enabledLanguages.map(lang => lang.tag)) || [];
|
||||
if (langTags.includes(detectedLangTag)) {
|
||||
return detectedLangTag;
|
||||
}
|
||||
|
||||
const defaultLanguage = idx(env, ['translation', 'defaultLanguage']);
|
||||
if (defaultLanguage && defaultLanguage.tag) {
|
||||
return defaultLanguage.tag;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function getVersion(filepath, refDir, env) {
|
||||
const versioningEnabled = idx(env, ['versioning', 'enabled']);
|
||||
|
||||
if (versioningEnabled) {
|
||||
const subFolder = getSubFolder(filepath, refDir);
|
||||
|
||||
if (subFolder) {
|
||||
const detectedVersion = subFolder.replace(/^version-/, '');
|
||||
const versions = idx(env, ['versioning', 'versions']) || [];
|
||||
if (versions.includes(detectedVersion)) {
|
||||
return detectedVersion;
|
||||
}
|
||||
}
|
||||
|
||||
return 'next';
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
module.exports = async function processMetadata(
|
||||
source,
|
||||
refDir,
|
||||
env,
|
||||
order,
|
||||
siteConfig,
|
||||
docsBasePath,
|
||||
) {
|
||||
const filepath = path.resolve(refDir, source);
|
||||
const fileString = await fs.readFile(filepath, 'utf-8');
|
||||
const {metadata} = parse(fileString);
|
||||
|
||||
// Default id is the file name.
|
||||
if (!metadata.id) {
|
||||
metadata.id = path.basename(source, path.extname(source));
|
||||
}
|
||||
if (metadata.id.includes('/')) {
|
||||
throw new Error('Document id cannot include "/".');
|
||||
}
|
||||
|
||||
// Default title is the id.
|
||||
if (!metadata.title) {
|
||||
metadata.title = metadata.id;
|
||||
}
|
||||
|
||||
// Language.
|
||||
const language = getLanguage(filepath, refDir, env);
|
||||
metadata.language = language;
|
||||
const langPart = (language && `${language}/`) || '';
|
||||
|
||||
// Version.
|
||||
const defaultLangTag = idx(env, ['translation', 'defaultLanguage', 'tag']);
|
||||
let versionRefDir = refDir;
|
||||
if (language && language !== defaultLangTag) {
|
||||
versionRefDir = path.join(refDir, language);
|
||||
}
|
||||
const version = getVersion(filepath, versionRefDir, env);
|
||||
metadata.version = version;
|
||||
const latestVersion = idx(env, ['versioning', 'latestVersion']);
|
||||
const versionPart =
|
||||
(version && version !== latestVersion && `${version}/`) || '';
|
||||
|
||||
// Convert temporarily metadata.id to the form of dirname/id without version/lang prefix.
|
||||
// e.g.: file `versioned_docs/version-1.0.0/en/foo/bar.md` with id `version-1.0.0-bar` => `foo/bar`
|
||||
if (language) {
|
||||
metadata.id = metadata.id.replace(new RegExp(`^${language}-`), '');
|
||||
}
|
||||
|
||||
if (version) {
|
||||
metadata.id = metadata.id.replace(new RegExp(`^version-${version}-`), '');
|
||||
}
|
||||
|
||||
const dirName = path.dirname(source);
|
||||
if (dirName !== '.') {
|
||||
let prefix = dirName;
|
||||
if (language) {
|
||||
prefix = prefix.replace(new RegExp(`^${language}`), '');
|
||||
}
|
||||
prefix = prefix.replace(/^\//, '');
|
||||
if (version) {
|
||||
prefix = prefix.replace(new RegExp(`^version-${version}`), '');
|
||||
}
|
||||
prefix = prefix.replace(/^\//, '');
|
||||
if (prefix) {
|
||||
metadata.id = `${prefix}/${metadata.id}`;
|
||||
}
|
||||
}
|
||||
|
||||
// The docs absolute file source.
|
||||
// e.g: `/end/docs/hello.md` or `/end/website/versioned_docs/version-1.0.0/hello.md`
|
||||
metadata.source = path.join(refDir, source);
|
||||
|
||||
// Build the permalink.
|
||||
const {baseUrl} = siteConfig;
|
||||
|
||||
// If user has own custom permalink defined in frontmatter
|
||||
// e.g: :baseUrl:docsUrl/:langPart/:versionPart/endiliey/:id
|
||||
if (metadata.permalink) {
|
||||
metadata.permalink = path.resolve(
|
||||
metadata.permalink
|
||||
.replace(/:baseUrl/, baseUrl)
|
||||
.replace(/:docsUrl/, docsBasePath)
|
||||
.replace(/:langPart/, langPart)
|
||||
.replace(/:versionPart/, versionPart)
|
||||
.replace(/:id/, metadata.id),
|
||||
);
|
||||
} else {
|
||||
metadata.permalink = normalizeUrl([
|
||||
baseUrl,
|
||||
docsBasePath,
|
||||
langPart,
|
||||
versionPart,
|
||||
metadata.id,
|
||||
]);
|
||||
}
|
||||
|
||||
// If version.
|
||||
if (version && version !== 'next') {
|
||||
metadata.id = `version-${version}-${metadata.id}`;
|
||||
}
|
||||
|
||||
// Save localized id before adding language on it.
|
||||
metadata.localized_id = metadata.id;
|
||||
|
||||
// If language.
|
||||
if (language) {
|
||||
metadata.id = `${language}-${metadata.id}`;
|
||||
}
|
||||
|
||||
// Determine order.
|
||||
const id = metadata.localized_id;
|
||||
if (order[id]) {
|
||||
metadata.sidebar = order[id].sidebar;
|
||||
metadata.category = order[id].category;
|
||||
metadata.subCategory = order[id].subCategory;
|
||||
if (order[id].next) {
|
||||
metadata.next_id = order[id].next;
|
||||
metadata.next = (language ? `${language}-` : '') + order[id].next;
|
||||
}
|
||||
if (order[id].previous) {
|
||||
metadata.previous_id = order[id].previous;
|
||||
metadata.previous = (language ? `${language}-` : '') + order[id].previous;
|
||||
}
|
||||
}
|
||||
|
||||
return metadata;
|
||||
};
|
72
packages/docusaurus-plugin-content-docs/src/order.js
Normal file
72
packages/docusaurus-plugin-content-docs/src/order.js
Normal file
|
@ -0,0 +1,72 @@
|
|||
/**
|
||||
* Copyright (c) 2017-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
// Build the docs meta such as next, previous, category and sidebar.
|
||||
module.exports = function createOrder(allSidebars = {}) {
|
||||
const order = {};
|
||||
|
||||
Object.keys(allSidebars).forEach(sidebarId => {
|
||||
const sidebar = allSidebars[sidebarId];
|
||||
|
||||
const ids = [];
|
||||
const categoryOrder = [];
|
||||
const subCategoryOrder = [];
|
||||
const indexItems = ({items, categoryLabel, subCategoryLabel}) => {
|
||||
items.forEach(item => {
|
||||
switch (item.type) {
|
||||
case 'category':
|
||||
indexItems({
|
||||
items: item.items,
|
||||
categoryLabel: categoryLabel || item.label,
|
||||
subCategoryLabel: categoryLabel && item.label,
|
||||
});
|
||||
break;
|
||||
case 'ref':
|
||||
case 'link':
|
||||
// Refs and links should not be shown in navigation.
|
||||
break;
|
||||
case 'doc':
|
||||
ids.push(item.id);
|
||||
categoryOrder.push(categoryLabel);
|
||||
subCategoryOrder.push(subCategoryLabel);
|
||||
break;
|
||||
default:
|
||||
throw new Error(
|
||||
`Unknown item type: ${item.type}. Item: ${JSON.stringify(item)}`,
|
||||
);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
indexItems({items: sidebar});
|
||||
|
||||
// eslint-disable-next-line
|
||||
for (let i = 0; i < ids.length; i++) {
|
||||
const id = ids[i];
|
||||
let previous;
|
||||
let next;
|
||||
|
||||
if (i > 0) {
|
||||
previous = ids[i - 1];
|
||||
}
|
||||
|
||||
if (i < ids.length - 1) {
|
||||
next = ids[i + 1];
|
||||
}
|
||||
|
||||
order[id] = {
|
||||
previous,
|
||||
next,
|
||||
sidebar: sidebarId,
|
||||
category: categoryOrder[i],
|
||||
subCategory: subCategoryOrder[i],
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
return order;
|
||||
};
|
150
packages/docusaurus-plugin-content-docs/src/sidebars.js
Normal file
150
packages/docusaurus-plugin-content-docs/src/sidebars.js
Normal file
|
@ -0,0 +1,150 @@
|
|||
/**
|
||||
* Copyright (c) 2017-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
const fs = require('fs-extra');
|
||||
const path = require('path');
|
||||
const {idx} = require('@docusaurus/utils');
|
||||
|
||||
/**
|
||||
* Check that item contains only allowed keys
|
||||
*
|
||||
* @param {Object} item
|
||||
* @param {Array<string>} keys
|
||||
*/
|
||||
function assertItem(item, keys) {
|
||||
const unknownKeys = Object.keys(item).filter(
|
||||
key => !keys.includes(key) && key !== 'type',
|
||||
);
|
||||
|
||||
if (unknownKeys.length) {
|
||||
throw new Error(
|
||||
`Unknown sidebar item keys: ${unknownKeys}. Item: ${JSON.stringify(
|
||||
item,
|
||||
)}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalizes recursively category and all its children. Ensures, that at the end
|
||||
* each item will be an object with the corresponding type
|
||||
*
|
||||
* @param {Array<Object>} category
|
||||
* @param {number} [level=0]
|
||||
*
|
||||
* @return {Array<Object>}
|
||||
*/
|
||||
function normalizeCategory(category, level = 0) {
|
||||
if (level === 2) {
|
||||
throw new Error(
|
||||
`Can not process ${
|
||||
category.label
|
||||
} category. Categories can be nested only one level deep.`,
|
||||
);
|
||||
}
|
||||
|
||||
assertItem(category, ['items', 'label']);
|
||||
|
||||
if (!Array.isArray(category.items)) {
|
||||
throw new Error(
|
||||
`Error loading ${category.label} category. Category items must be array.`,
|
||||
);
|
||||
}
|
||||
|
||||
const items = category.items.map(item => {
|
||||
switch (item.type) {
|
||||
case 'category':
|
||||
return normalizeCategory(item, level + 1);
|
||||
case 'link':
|
||||
assertItem(item, ['href', 'label']);
|
||||
break;
|
||||
case 'ref':
|
||||
assertItem(item, ['id', 'label']);
|
||||
break;
|
||||
default:
|
||||
if (typeof item === 'string') {
|
||||
return {
|
||||
type: 'doc',
|
||||
id: item,
|
||||
};
|
||||
}
|
||||
|
||||
if (item.type !== 'doc') {
|
||||
throw new Error(`Unknown sidebar item type: ${item.type}`);
|
||||
}
|
||||
|
||||
assertItem(item, ['id', 'label']);
|
||||
break;
|
||||
}
|
||||
|
||||
return item;
|
||||
});
|
||||
|
||||
return {...category, items};
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts sidebars object to mapping to arrays of sidebar item objects
|
||||
*
|
||||
* @param {{[key: string]: Object}} sidebars
|
||||
*
|
||||
* @return {{[key: string]: Array<Object>}}
|
||||
*/
|
||||
function normalizeSidebar(sidebars) {
|
||||
return Object.entries(sidebars).reduce((acc, [sidebarId, sidebar]) => {
|
||||
let normalizedSidebar = sidebar;
|
||||
|
||||
if (!Array.isArray(sidebar)) {
|
||||
// convert sidebar to a more generic structure
|
||||
normalizedSidebar = Object.entries(sidebar).map(([label, items]) => ({
|
||||
type: 'category',
|
||||
label,
|
||||
items,
|
||||
}));
|
||||
}
|
||||
|
||||
acc[sidebarId] = normalizedSidebar.map(item => normalizeCategory(item));
|
||||
|
||||
return acc;
|
||||
}, {});
|
||||
}
|
||||
|
||||
module.exports = function loadSidebars({siteDir, env}, deleteCache = true) {
|
||||
let allSidebars = {};
|
||||
|
||||
// current sidebars
|
||||
const sidebarsJSONFile = path.join(siteDir, 'sidebars.json');
|
||||
if (deleteCache) {
|
||||
delete require.cache[sidebarsJSONFile];
|
||||
}
|
||||
if (fs.existsSync(sidebarsJSONFile)) {
|
||||
allSidebars = require(sidebarsJSONFile); // eslint-disable-line
|
||||
}
|
||||
|
||||
// versioned sidebars
|
||||
if (idx(env, ['versioning', 'enabled'])) {
|
||||
const versions = idx(env, ['versioning', 'versions']);
|
||||
if (Array.isArray(versions)) {
|
||||
versions.forEach(version => {
|
||||
const versionedSidebarsJSONFile = path.join(
|
||||
siteDir,
|
||||
'versioned_sidebars',
|
||||
`version-${version}-sidebars.json`,
|
||||
);
|
||||
if (fs.existsSync(versionedSidebarsJSONFile)) {
|
||||
const sidebar = require(versionedSidebarsJSONFile); // eslint-disable-line
|
||||
Object.assign(allSidebars, sidebar);
|
||||
} else {
|
||||
const missingFile = path.relative(siteDir, versionedSidebarsJSONFile);
|
||||
throw new Error(`Failed to load ${missingFile}. It does not exist.`);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return normalizeSidebar(allSidebars);
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue