Skip to content
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
1 change: 0 additions & 1 deletion packages/create-docusaurus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
"commander": "^5.1.0",
"execa": "^5.1.1",
"fs-extra": "^11.1.1",
"lodash": "^4.17.21",
"prompts": "^2.4.2",
"semver": "^7.5.4",
"supports-color": "^9.4.0",
Expand Down
42 changes: 42 additions & 0 deletions packages/create-docusaurus/src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import {siteNameToPackageName} from '../utils';

describe('siteNameToPackageName', () => {
it('converts simple cases', () => {
const testCases: [string, string][] = [
['Foo Bar', 'foo-bar'],
['fooBar', 'foo-bar'],
['__FOO_BAR__', 'foo-bar'],
['XMLHttpRequest', 'xml-http-request'],
['sitemapXML', 'sitemap-xml'],
['XMLHttp', 'xml-http'],
['xml-http', 'xml-http'],
];

testCases.forEach(([input, expected]) => {
expect(siteNameToPackageName(input)).toEqual(expected);
});
});

it('converts ñ', () => {
expect(siteNameToPackageName('mañanaFoo')).toEqual('ma-ana-foo');
});

it('converts __', () => {
expect(siteNameToPackageName('foo__bar')).toEqual('foo-bar');
});

it('skips 🔥', () => {
expect(siteNameToPackageName('🔥')).toEqual('🔥');
});

it('skips !!!', () => {
expect(siteNameToPackageName('!!!')).toEqual('!!!');
});
});
14 changes: 11 additions & 3 deletions packages/create-docusaurus/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import fs from 'fs-extra';
import {fileURLToPath} from 'url';
import path from 'path';
import _ from 'lodash';
import {logger} from '@docusaurus/logger';
import execa from 'execa';
import prompts, {type Choice} from 'prompts';
Expand All @@ -17,6 +16,7 @@ import supportsColor from 'supports-color';
// TODO remove dependency on large @docusaurus/utils
// would be better to have a new smaller @docusaurus/utils-cli package
import {askPreferredLanguage} from '@docusaurus/utils';
import {siteNameToPackageName} from './utils.js';

type LanguagesOptions = {
javascript?: boolean;
Expand Down Expand Up @@ -164,7 +164,15 @@ async function readTemplates(): Promise<Template[]> {
);

// Classic should be first in list!
return _.sortBy(templates, (t) => t.name !== recommendedTemplate);
return templates.sort((a, b) => {
if (a.name === recommendedTemplate) {
return -1;
}
if (b.name === recommendedTemplate) {
return 1;
}
return 0;
});
}

async function copyTemplate(
Expand Down Expand Up @@ -562,7 +570,7 @@ export default async function init(
// Update package.json info.
try {
await updatePkg(path.join(dest, 'package.json'), {
name: _.kebabCase(siteName),
name: siteNameToPackageName(siteName),
version: '0.0.0',
private: true,
});
Expand Down
23 changes: 23 additions & 0 deletions packages/create-docusaurus/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

/**
* We use a simple kebab-case-like conversion
* It's not perfect, but good enough
* We don't want to depend on lodash in this package
* See https://github.com/facebook/docusaurus/pull/11653
* @param siteName
*/
export function siteNameToPackageName(siteName: string): string {
const match = siteName.match(
/[A-Z]{2,}(?=[A-Z][a-z]+\d*|\b|_)|[A-Z]?[a-z]+\d*|[A-Z]|\d+/g,
);
if (match) {
return match.map((x) => x.toLowerCase()).join('-');
}
return siteName;
}
33 changes: 33 additions & 0 deletions packages/docusaurus-plugin-content-pages/src/contentHelpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import type {LoadedContent, Metadata} from '@docusaurus/plugin-content-pages';

function indexPagesBySource(content: LoadedContent): Map<string, Metadata> {
return new Map(content.map((page) => [page.source, page]));
}

// TODO this is bad, we should have a better way to do this (new lifecycle?)
// The source to page/permalink is a mutable map passed to the mdx loader
// See https://github.com/facebook/docusaurus/pull/10457
// See https://github.com/facebook/docusaurus/pull/10185
export function createContentHelpers() {
const sourceToPage = new Map<string, Metadata>();
const sourceToPermalink = new Map<string, string>();

// Mutable map update :/
function updateContent(content: LoadedContent): void {
sourceToPage.clear();
sourceToPermalink.clear();
indexPagesBySource(content).forEach((value, key) => {
sourceToPage.set(key, value);
sourceToPermalink.set(key, value.permalink);
});
}

return {updateContent, sourceToPage, sourceToPermalink};
}
12 changes: 12 additions & 0 deletions packages/docusaurus-plugin-content-pages/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ import {
addTrailingPathSeparator,
createAbsoluteFilePathMatcher,
getContentPathList,
resolveMarkdownLinkPathname,
} from '@docusaurus/utils';
import {createMDXLoaderRule} from '@docusaurus/mdx-loader';
import {createAllRoutes} from './routes';
import {createPagesContentPaths, loadPagesContent} from './content';
import {createContentHelpers} from './contentHelpers';
import type {LoadContext, Plugin} from '@docusaurus/types';
import type {
PluginOptions,
Expand All @@ -32,6 +34,7 @@ export default async function pluginContentPages(
const {siteConfig, siteDir, generatedFilesDir} = context;

const contentPaths = createPagesContentPaths({context, options});
const contentHelpers = createContentHelpers();

const pluginDataDirRoot = path.join(
generatedFilesDir,
Expand Down Expand Up @@ -82,6 +85,14 @@ export default async function pluginContentPages(
image: frontMatter.image,
}),
markdownConfig: siteConfig.markdown,
resolveMarkdownLink: ({linkPathname, sourceFilePath}) => {
return resolveMarkdownLinkPathname(linkPathname, {
sourceFilePath,
sourceToPermalink: contentHelpers.sourceToPermalink,
siteDir,
contentPaths,
});
},
},
});
}
Expand Down Expand Up @@ -109,6 +120,7 @@ export default async function pluginContentPages(
if (!content) {
return;
}
contentHelpers.updateContent(content);
await createAllRoutes({content, options, actions});
},

Expand Down
1 change: 1 addition & 0 deletions website/_dogfooding/_pages tests/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ import Readme from "../README.mdx"
- [Embeds](/tests/pages/embeds)
- [Style Isolation tests](/tests/pages/style-isolation)
- [IdealImage tests](/tests/pages/ideal-image)
- [Linking tests](./linking/index.md)
26 changes: 26 additions & 0 deletions website/_dogfooding/_pages tests/linking/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
title: Markdown Linking Test
description: Test pages to verify markdown file path links work in the pages plugin
---

# Linking Test

This folder contains test pages to verify markdown file path links work correctly in the `@docusaurus/plugin-content-pages`.

## Relative file path links

- [`./index.md`](./index.md)
- [`./page-a.md`](./page-a.md)
- [`./nested/page-b.md`](./nested/page-b.md)

## Absolute file path links

- [`/index.md`](/linking/index.md)
- [`/page-a.md`](/linking/page-a.md)
- [`/nested/page-b.md`](/linking/nested/page-b.md)

## Site alias file path links

- [`@site/_dogfooding/_pages tests/linking/index.md`](<@site/_dogfooding/_pages tests/linking/index.md>)
- [`@site/_dogfooding/_pages tests/linking/page-a.md`](<@site/_dogfooding/_pages tests/linking/page-a.md>)
- [`@site/_dogfooding/_pages tests/linking/nested/page-b.md`](<@site/_dogfooding/_pages tests/linking/nested/page-b.md>)
20 changes: 20 additions & 0 deletions website/_dogfooding/_pages tests/linking/nested/page-b.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Linking Test - Page B

## Relative file path links

- [`../index.md`](../index.md)
- [`../page-a.md`](../page-a.md)
- [`./page-b.md`](./page-b.md)
- [`../nested/page-b.md`](../nested/page-b.md)

## Absolute file path links

- [`/index.md`](/linking/index.md)
- [`/page-a.md`](/linking/page-a.md)
- [`/nested/page-b.md`](/linking/nested/page-b.md)

## Site alias file path links

- [`@site/_dogfooding/_pages tests/linking/index.md`](<@site/_dogfooding/_pages tests/linking/index.md>)
- [`@site/_dogfooding/_pages tests/linking/page-a.md`](<@site/_dogfooding/_pages tests/linking/page-a.md>)
- [`@site/_dogfooding/_pages tests/linking/nested/page-b.md`](<@site/_dogfooding/_pages tests/linking/nested/page-b.md>)
19 changes: 19 additions & 0 deletions website/_dogfooding/_pages tests/linking/page-a.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Linking Test - Page A

## Relative file path links

- [`./index.md`](./index.md)
- [`./page-a.md`](./page-a.md)
- [`./nested/page-b.md`](./nested/page-b.md)

## Absolute file path links

- [`/index.md`](/linking/index.md)
- [`/page-a.md`](/linking/page-a.md)
- [`/nested/page-b.md`](/linking/nested/page-b.md)

## Site alias file path links

- [`@site/_dogfooding/_pages tests/linking/index.md`](<@site/_dogfooding/_pages tests/linking/index.md>)
- [`@site/_dogfooding/_pages tests/linking/page-a.md`](<@site/_dogfooding/_pages tests/linking/page-a.md>)
- [`@site/_dogfooding/_pages tests/linking/nested/page-b.md`](<@site/_dogfooding/_pages tests/linking/nested/page-b.md>)
Loading