Skip to content

feat: allow starting linenumbers from set value #19

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions pages/code-view/with-line-numbers.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export default function CodeViewPage() {
<SpaceBetween direction="vertical" size="l">
<CodeView lineNumbers={true} content={`# Hello World`} />
<CodeView lineNumbers={true} content={`# Hello World\n\nThis is Cloudscape.`} />
<CodeView lineNumbers={true} lineNumbersStart={5} content={`# Hello World\n\nThis is Cloudscape.`} />
<CodeView lineNumbers={true} lineNumbersStart={-1} content={`# Hello World\n\nThis is Cloudscape.`} />
</SpaceBetween>
</ScreenshotArea>
);
Expand Down
8 changes: 8 additions & 0 deletions src/__tests__/__snapshots__/documenter.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ Defaults to \`false\`.
"optional": true,
"type": "boolean",
},
{
"description": "Controls the number the line numbers start counting from.
Defaults to \`1\`.
",
"name": "lineNumbersStart",
"optional": true,
"type": "number",
},
],
"regions": [
{
Expand Down
12 changes: 12 additions & 0 deletions src/code-view/__tests__/code-view.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ describe("CodeView", () => {
expect(wrapper!.findByClassName(styles["line-numbers"])!.getElement()).toHaveTextContent("123");
});

test("correctly renders line numbers when lineNumbersStart is set", () => {
render(<CodeView content={`Hello\nWorld\n!`} lineNumbers={true} lineNumbersStart={4}></CodeView>);
const wrapper = createWrapper()!.findCodeView();
expect(wrapper!.findByClassName(styles["line-numbers"])!.getElement()).toHaveTextContent("456");
});

test("correctly renders line numbers when lineNumbersStart is set to a negative value", () => {
render(<CodeView content={`Hello\nWorld\n!`} lineNumbers={true} lineNumbersStart={-1}></CodeView>);
const wrapper = createWrapper()!.findCodeView();
expect(wrapper!.findByClassName(styles["line-numbers"])!.getElement()).toHaveTextContent("-101");
});

test("correctly renders aria-label", () => {
render(<CodeView content={`Hello\nWorld\n!`} ariaLabel="Code snippet"></CodeView>);
const wrapper = createWrapper()!.findCodeView();
Expand Down
7 changes: 7 additions & 0 deletions src/code-view/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ export interface CodeViewProps {
*/
lineNumbers?: boolean;

/**
* Controls the number the line numbers start counting from.
*
* Defaults to `1`.
*/
lineNumbersStart?: number;

/**
* An optional slot to display a button to enable users to perform actions, such as copy or download the code snippet.
*
Expand Down
7 changes: 4 additions & 3 deletions src/code-view/internal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import styles from "./styles.css.js";

const ACE_CLASSES = { light: "ace-cloud_editor", dark: "ace-cloud_editor_dark" };

function getLineNumbers(content: string) {
return content.split("\n").map((_, n) => n + 1);
function getLineNumbers(content: string, lineNumbersStart = 1) {
return content.split("\n").map((_, n) => lineNumbersStart + n);
}

type InternalCodeViewProps = CodeViewProps & InternalBaseComponentProps;
Expand All @@ -20,6 +20,7 @@ export function InternalCodeView({
content,
actions,
lineNumbers,
lineNumbersStart,
highlight,
ariaLabel,
ariaLabelledby,
Expand All @@ -46,7 +47,7 @@ export function InternalCodeView({
<Box color="text-status-inactive" fontSize="body-m">
{lineNumbers && (
<div className={styles["line-numbers"]} aria-hidden={true}>
{getLineNumbers(content).map((number) => (
{getLineNumbers(content, lineNumbersStart).map((number) => (
<span key={number}>{number}</span>
))}
</div>
Expand Down
Loading