Skip to content

Commit

Permalink
fix(line-numbers): reset table styles, adjust column width (#254)
Browse files Browse the repository at this point in the history
* chore: fix typos

* fix(line-numbers): reset table styles, adjust column width

* test: update snapshot
  • Loading branch information
metonym authored Dec 30, 2022
1 parent 280ac45 commit a477780
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ Use the `LineNumbers` component to render the highlighted code with line numbers

```svelte
<script>
import Hightlight, { LineNumbers } from "svelte-highlight";
import Highlight, { LineNumbers } from "svelte-highlight";
import typescript from "svelte-highlight/languages/typescript";
import atomOneDark from "svelte-highlight/styles/atom-one-dark";
Expand Down Expand Up @@ -225,7 +225,7 @@ Use `--style-props` to customize styles.
{highlighted}
--line-number-color="pink"
--border-color="rgba(255, 255, 255, 0.2)"
--padding-left="0"
--padding-left={0}
--padding-right="3em"
/>
</Highlight>
Expand Down
2 changes: 1 addition & 1 deletion demo/lib/LineNumbers/Basic.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import atomOneDark from "svelte-highlight/styles/atom-one-dark";
const code = `<script>
import Hightlight, { LineNumbers } from "svelte-highlight";
import Highlight, { LineNumbers } from "svelte-highlight";
import typescript from "svelte-highlight/languages/typescript";
import atomOneDark from "svelte-highlight/styles/atom-one-dark";
Expand Down
26 changes: 22 additions & 4 deletions src/LineNumbers.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@
export let wrapLines = false;
const DIGIT_WIDTH = 18;
const MIN_DIGITS = 3;
const DIGIT_WIDTH = 12;
const MIN_DIGITS = 2;
$: lines = highlighted.split("\n");
$: len_digits = lines.length.toString().length;
Expand All @@ -70,7 +70,7 @@
</script>

<div style:overflow-x="auto" {...$$restProps}>
<table style:width="100%">
<table>
<tbody class:hljs={true}>
{#each lines as line, i}
{@const lineNumber = i + 1}
Expand All @@ -87,7 +87,6 @@
style:padding-top={isFirst ? "1em" : undefined}
style:padding-bottom={isLast ? "1em" : undefined}
style:width={width + "px"}
style:min-width={width + "px"}
>
<code style:color="var(--line-number-color, currentColor)">
{lineNumber}
Expand All @@ -103,6 +102,25 @@
</div>

<style>
pre {
margin: 0;
}
table,
tr,
td {
padding: 0;
border: 0;
margin: 0;
vertical-align: baseline;
}
table {
width: 100%;
border-collapse: collapse;
border-spacing: 0;
}
td {
padding-left: var(--padding-left, 1em);
padding-right: var(--padding-right, 1em);
Expand Down
6 changes: 3 additions & 3 deletions tests/SvelteHighlight.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ describe("SvelteHighlight", () => {

expect(target.querySelector("#line-numbers")?.innerHTML)
.toMatchInlineSnapshot(`
"<div style=\\"overflow-x: auto;\\" class=\\"svelte-1gt16c9\\"><table style=\\"width: 100%;\\"><tbody class=\\"hljs\\"><tr><td class=\\"svelte-1gt16c9 hljs\\" style=\\"position: sticky; left: 0px; text-align: right; user-select: none; padding-top: 1em; padding-bottom: 1em; width: 54px; min-width: 54px;\\"><code>1</code></td> <td class=\\"svelte-1gt16c9\\"><pre class=\\"svelte-1gt16c9\\"><code>
</code></pre></td> </tr></tbody></table></div>"
`);
"<div style=\\"overflow-x: auto;\\" class=\\"svelte-2e2lav\\"><table class=\\"svelte-2e2lav\\"><tbody class=\\"hljs\\"><tr class=\\"svelte-2e2lav\\"><td class=\\"svelte-2e2lav hljs\\" style=\\"position: sticky; left: 0px; text-align: right; user-select: none; padding-top: 1em; padding-bottom: 1em; width: 24px;\\"><code>1</code></td> <td class=\\"svelte-2e2lav\\"><pre class=\\"svelte-2e2lav\\"><code>
</code></pre></td> </tr></tbody></table></div>"
`);
});
});

0 comments on commit a477780

Please sign in to comment.