Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add includeIndex option #195

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -60,6 +60,7 @@ module.exports = {
port: 9000,
openBrowser: true,
paramType: 'search', // default is 'hash'
includeIndex: true, // default is false, explicitly includes 'index.html' in links.
exampleCode: `
<Button>
Hello World!
Expand Down
1 change: 1 addition & 0 deletions src/index.d.ts
Expand Up @@ -13,6 +13,7 @@ interface PlayroomConfig {
baseUrl?: string;
paramType: 'hash' | 'search';
iframeSandbox?: string;
includeIndex?: boolean;
}

interface InternalPlayroomConfig extends PlayroomConfig {
Expand Down
1 change: 1 addition & 0 deletions src/utils/usePreviewUrl.ts
Expand Up @@ -18,5 +18,6 @@ export default (theme: string) => {
code,
theme: isThemed ? theme : undefined,
paramType: playroomConfig.paramType,
includeIndex: playroomConfig.includeIndex,
});
};
2 changes: 2 additions & 0 deletions utils/index.d.ts
Expand Up @@ -22,6 +22,7 @@ interface CreateUrlOptions {
themes?: string[];
widths?: number[];
paramType?: ParamType;
includeIndex?: boolean;
}

export const createUrl: (options: CreateUrlOptions) => string;
Expand All @@ -31,6 +32,7 @@ interface CreatePreviewUrlOptions {
code?: string;
theme?: string;
paramType?: ParamType;
includeIndex?: boolean;
}

export const createPreviewUrl: (options: CreatePreviewUrlOptions) => string;
25 changes: 21 additions & 4 deletions utils/index.js
Expand Up @@ -11,13 +11,22 @@ const compressParams = ({ code, themes, widths, theme }) => {
return lzString.compressToEncodedURIComponent(data);
};

const createUrl = ({ baseUrl, code, themes, widths, paramType = 'hash' }) => {
const createUrl = ({
baseUrl,
code,
themes,
widths,
paramType = 'hash',
includeIndex = false,
}) => {
let path = '';

if (code || themes || widths) {
const compressedData = compressParams({ code, themes, widths });

path = `${paramType === 'hash' ? '#' : ''}?code=${compressedData}`;
path = `${includeIndex ? '/index.html' : ''}${
paramType === 'hash' ? '#' : ''
}?code=${compressedData}`;
}

if (baseUrl) {
Expand All @@ -29,13 +38,21 @@ const createUrl = ({ baseUrl, code, themes, widths, paramType = 'hash' }) => {
return path;
};

const createPreviewUrl = ({ baseUrl, code, theme, paramType = 'hash' }) => {
const createPreviewUrl = ({
baseUrl,
code,
theme,
paramType = 'hash',
includeIndex = false,
}) => {
let path = '';

if (code || theme) {
const compressedData = compressParams({ code, theme });

path = `/preview${paramType === 'hash' ? '#' : ''}?code=${compressedData}`;
path = `/preview${includeIndex ? '/index.html' : ''}${
paramType === 'hash' ? '#' : ''
}?code=${compressedData}`;
}

if (baseUrl) {
Expand Down