escape the url of the addon file for the browser

This commit is contained in:
Norbert de Langen 2022-10-07 13:33:01 +02:00
parent ca7dd1dc32
commit 662e2510da
No known key found for this signature in database
GPG Key ID: FD0E78AF9A837762
2 changed files with 9 additions and 9 deletions

View File

@ -4,16 +4,16 @@ test('sanatizePath', () => {
const addonsDir = '/Users/username/Projects/projectname/storybook'; const addonsDir = '/Users/username/Projects/projectname/storybook';
const text = 'demo text'; const text = 'demo text';
const file = { const file = {
path: '/Users/username/Projects/projectname/storybook/node_modules/@storybook/addon-x/dist/manager.mjs', path: '/Users/username/Projects/projectname/storybook/node_modules/@storybook/addon-x+y/dist/manager.mjs',
contents: Uint8Array.from(Array.from(text).map((letter) => letter.charCodeAt(0))), contents: Uint8Array.from(Array.from(text).map((letter) => letter.charCodeAt(0))),
text, text,
}; };
const { location, url } = sanatizePath(file, addonsDir); const { location, url } = sanatizePath(file, addonsDir);
expect(location).toMatchInlineSnapshot( expect(location).toMatchInlineSnapshot(
`"/Users/username/Projects/projectname/storybook/node-modules-storybook-addon-x-dist-manager.mjs"` `"/Users/username/Projects/projectname/storybook/node_modules/@storybook/addon-x+y/dist/manager.mjs"`
); );
expect(url).toMatchInlineSnapshot( expect(url).toMatchInlineSnapshot(
`"./sb-addons/node-modules-storybook-addon-x-dist-manager.mjs"` `"./sb-addons/node_modules/%40storybook/addon-x%2By/dist/manager.mjs"`
); );
}); });

View File

@ -3,7 +3,10 @@ import { writeFile, ensureFile } from 'fs-extra';
import { join } from 'path'; import { join } from 'path';
import { Compilation } from '../types'; import { Compilation } from '../types';
export async function readOrderedFiles(addonsDir: string, outputFiles: Compilation['outputFiles'] | undefined) { export async function readOrderedFiles(
addonsDir: string,
outputFiles: Compilation['outputFiles'] | undefined
) {
const files = await Promise.all( const files = await Promise.all(
outputFiles?.map(async (file) => { outputFiles?.map(async (file) => {
// convert deeply nested paths to a single level, also remove special characters // convert deeply nested paths to a single level, also remove special characters
@ -20,11 +23,8 @@ export async function readOrderedFiles(addonsDir: string, outputFiles: Compilati
} }
export function sanatizePath(file: OutputFile, addonsDir: string) { export function sanatizePath(file: OutputFile, addonsDir: string) {
const filePath = file.path const filePath = file.path.replace(addonsDir, '');
.replace(addonsDir, '')
.replace(/[^a-z0-9\-.]+/g, '-')
.replace(/^-/, '/');
const location = join(addonsDir, filePath); const location = join(addonsDir, filePath);
const url = `./sb-addons${filePath}`; const url = `./sb-addons${filePath.split('/').map(encodeURIComponent).join('/')}`;
return { location, url }; return { location, url };
} }