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

fix: encode demo.id to avoid route breaking #1613

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
23 changes: 15 additions & 8 deletions src/client/theme-api/DumiDemo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,25 @@ export const DumiDemo: FC<IDumiDemoProps> = (props) => {
}

const isHashRoute = historyType === 'hash';

// allow user override demoUrl by frontmatter
let demoUrl =
props.previewerProps.demoUrl ||
// when use hash route, browser can automatically handle relative paths starting with #
`${isHashRoute ? `#` : ''}${basename}${SP_ROUTE_PREFIX}demos/${props.demo.id}`;

const [, prefix = '', demoId] =
new RegExp(`(.*)${SP_ROUTE_PREFIX}demos/(.*)$`).exec(props.previewerProps.demoUrl || '') ||
[];
if (demoId) {
// encode demo.id to avoid slash in id breaking the route
demoUrl = `${prefix}${SP_ROUTE_PREFIX}demos/${encodeURIComponent(demoId)}`;
Copy link
Member

Choose a reason for hiding this comment

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

什么情况下 id 会包含 /

Copy link
Contributor Author

Choose a reason for hiding this comment

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

手动指定了 docDirs

}

return (
<Previewer
asset={asset}
demoUrl={
// allow user override demoUrl by frontmatter
props.previewerProps.demoUrl ||
// when use hash route, browser can automatically handle relative paths starting with #
`${isHashRoute ? `#` : ''}${basename}${SP_ROUTE_PREFIX}demos/${
props.demo.id
}`
}
demoUrl={demoUrl}
{...props.previewerProps}
>
{props.previewerProps.iframe ? null : (
Expand Down