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

Copy only commands in command snippets #352

Merged
merged 1 commit into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
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
21 changes: 16 additions & 5 deletions components/MDX/Pre.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,25 @@ const Pre = ({ children, className }: CodeProps) => {
}
}

document.body.appendChild(copyText);
const processedInnerText = toCopyContent(copyText, [
"." + commandStyles.line,
"." + codeStyles.line,
// Assemble an array of class names of elements within copyText to copy
// when a user clicks the copy button.
let classesToCopy = [
// Class name added by rehype-highlight to a `code` element when
// highlighting syntax in code snippets
".hljs",
]);
];

// If copyText includes at least one CommandLine, the intention is for
// users to copy commands and not example outputs (CodeLines). If there
// are no CommandLines, it is fine to copy the CodeLines.
if (copyText.getElementsByClassName(commandStyles.line).length > 0) {
classesToCopy.push("." + commandStyles.line);
} else {
classesToCopy.push("." + codeStyles.line);
}

document.body.appendChild(copyText);
const processedInnerText = toCopyContent(copyText, classesToCopy);

navigator.clipboard.writeText(processedInnerText);
document.body.removeChild(copyText);
Expand Down
34 changes: 33 additions & 1 deletion components/Snippet/Snippet.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { expect } from "@storybook/jest";

import { Var } from "../Variables/Var";
import { default as Snippet } from "./Snippet";
import Command, { CommandLine } from "../Command/Command";
import Command, { CommandLine, CommandComment } from "../Command/Command";
import { CodeLine } from "../Code";
import { replaceClipboardWithCopyBuffer } from "utils/clipboard";

Expand Down Expand Up @@ -55,6 +55,38 @@ export const CopyCommandVar: Story = {
},
};

// A code snippet with commands should only copy the commands.
export const CopyCommandVarWithOutput: Story = {
render: () => {
return (
<Snippet>
<Command>
<CommandLine data-content="$ ">
curl https://
<Var name="example.com" isGlobal="false" description="" />
/v1/webapi/saml/acs/azure-saml
</CommandLine>
</Command>
<CodeLine>
The output of curling <Var name="example.com" />
</CodeLine>
</Snippet>
);
},
play: async ({ canvasElement, step }) => {
replaceClipboardWithCopyBuffer();
const canvas = within(canvasElement);

await step("Copy the content", async () => {
await userEvent.click(canvas.getByTestId("copy-button-all"));
expect(navigator.clipboard.readText()).toEqual(
"curl https://example.com/v1/webapi/saml/acs/azure-saml"
);
});
},
};

// A code snippet with no commands should copy all content within the snippet.
export const CopyCodeLineVar: Story = {
render: () => {
return (
Expand Down
Loading