mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-28 08:27:03 +02:00
feat: multiple playground choices (#5207)
* poc of using netlify functions for playground redirections * push * cleanup * restore files * fix netlify functions? * fix netlify functions? * implement serverless functions for playgrounds with persistent cookie * move new.docusaurus.io to monorepo packages * move new.docusaurus.io to monorepo packages * lockfile * push * catch-all redirect * Translate/Interpolate: add better error message if not used correctly * Add /docs/playground page * Add some additional doc
This commit is contained in:
parent
2b5fd2b490
commit
700a82aefe
19 changed files with 1426 additions and 129 deletions
77
admin/new.docusaurus.io/functionUtils/playgroundUtils.ts
Normal file
77
admin/new.docusaurus.io/functionUtils/playgroundUtils.ts
Normal file
|
@ -0,0 +1,77 @@
|
|||
/**
|
||||
* 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 {HandlerEvent, HandlerResponse} from '@netlify/functions';
|
||||
|
||||
const CookieName = 'DocusaurusPlaygroundName';
|
||||
|
||||
const PlaygroundConfigs = {
|
||||
codesandbox: 'https://codesandbox.io/s/docusaurus',
|
||||
stackblitz: 'https://stackblitz.com/fork/docusaurus',
|
||||
};
|
||||
|
||||
const PlaygroundDocumentationUrl = 'https://docusaurus.io/docs/playground';
|
||||
|
||||
export type PlaygroundName = keyof typeof PlaygroundConfigs;
|
||||
|
||||
function isValidPlaygroundName(
|
||||
playgroundName: string,
|
||||
): playgroundName is PlaygroundName {
|
||||
return Object.keys(PlaygroundConfigs).includes(playgroundName);
|
||||
}
|
||||
|
||||
export function createPlaygroundDocumentationResponse(): HandlerResponse {
|
||||
return {
|
||||
statusCode: 302,
|
||||
headers: {
|
||||
Location: PlaygroundDocumentationUrl,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function createPlaygroundResponse(
|
||||
playgroundName: PlaygroundName,
|
||||
): HandlerResponse {
|
||||
const playgroundUrl = PlaygroundConfigs[playgroundName];
|
||||
return {
|
||||
statusCode: 302,
|
||||
headers: {
|
||||
Location: playgroundUrl,
|
||||
'Set-Cookie': `${CookieName}=${playgroundName}`,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
// Inspired by https://stackoverflow.com/a/3409200/82609
|
||||
function parseCookieString(cookieString: string): Record<string, string> {
|
||||
const result: Record<string, string> = {};
|
||||
cookieString.split(';').forEach(function (cookie) {
|
||||
const [name, value] = cookie.split('=');
|
||||
result[name.trim()] = decodeURI(value);
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
export function readPlaygroundName(
|
||||
event: HandlerEvent,
|
||||
): PlaygroundName | undefined {
|
||||
const parsedCookie: Record<string, string> = event.headers.cookie
|
||||
? parseCookieString(event.headers.cookie)
|
||||
: {};
|
||||
const playgroundName: string | undefined = parsedCookie[CookieName];
|
||||
|
||||
if (playgroundName) {
|
||||
if (isValidPlaygroundName(playgroundName)) {
|
||||
return playgroundName;
|
||||
} else {
|
||||
console.error(
|
||||
`playgroundName found in cookie was invalid: ${playgroundName}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue