Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 11 additions & 4 deletions packages/create-docusaurus/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@
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';
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 {askPreferredLanguage, kebabCase} from '@docusaurus/utils';

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in the TODO, we want to remove the dependency on this package


type LanguagesOptions = {
javascript?: boolean;
Expand Down Expand Up @@ -164,7 +163,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 +569,7 @@ export default async function init(
// Update package.json info.
try {
await updatePkg(path.join(dest, 'package.json'), {
name: _.kebabCase(siteName),
name: kebabCase(siteName),
version: '0.0.0',
private: true,
});
Expand Down
20 changes: 19 additions & 1 deletion packages/docusaurus-utils/src/__tests__/jsUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import {jest} from '@jest/globals';
import _ from 'lodash';
import {mapAsyncSequential, findAsyncSequential} from '../jsUtils';
import {mapAsyncSequential, findAsyncSequential, kebabCase} from '../jsUtils';

describe('mapAsyncSequential', () => {
function sleep(timeout: number): Promise<void> {
Expand Down Expand Up @@ -80,3 +80,21 @@ describe('findAsyncSequential', () => {
expect(timeTotal).toBeLessThan(1000);
});
});

describe('kebabCase', () => {
it('conversion', () => {
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(kebabCase(input)).toEqual(expected);
});
});
});
2 changes: 1 addition & 1 deletion packages/docusaurus-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export {
getPluginI18nPath,
getLocaleConfig,
} from './i18nUtils';
export {mapAsyncSequential, findAsyncSequential} from './jsUtils';
export {mapAsyncSequential, findAsyncSequential, kebabCase} from './jsUtils';
export {
normalizeUrl,
getEditUrl,
Expand Down
11 changes: 11 additions & 0 deletions packages/docusaurus-utils/src/jsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,14 @@ export async function findAsyncSequential<T>(
}
return undefined;
}

/**
* Converts a string to kebab-case.
* Mimics lodash's behavior.
*/
export function kebabCase(str: string): string {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a reliable kebabCase implementation (compared to Lodash), so we clearly don't want to expose it as a js util to use everywhere else. The Lodash implementation remains better, and this good-enough implementation can stay within the create-docusaurus package.

const match = str.match(
/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b|[_])|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g,
);
return match ? match.map((x) => x.toLowerCase()).join('-') : '';
}