Skip to content

Commit

Permalink
Fix lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
davepagurek committed Mar 5, 2024
1 parent bb6fba8 commit 888fbe1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
18 changes: 18 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
},
"devDependencies": {
"@codemirror/lang-javascript": "^6.2.2",
"@types/jsdom": "^21.1.6",
"@types/node": "^20.11.19",
"@typescript-eslint/eslint-plugin": "^7.0.2",
"@typescript-eslint/parser": "^7.0.2",
Expand Down
20 changes: 10 additions & 10 deletions src/components/AnnotatedCode/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ import { codeToHtml } from 'shiki';
import { JSDOM } from 'jsdom';
const { props } = Astro;
const begin = (name) => `__SLOT_BEGIN_${name}`;
const end = (name) => `__SLOT_END_${name}`;
const begin = (name: string) => `__SLOT_BEGIN_${name}`;
const end = (name: string) => `__SLOT_END_${name}`;
const rawCode = props.code({ begin, end });
// Split raw lines into blocks separated by begin/end (and regions between)
const rawLines = rawCode.split(/\n/g);
let currentLine = 0;
const rows = [];
let currentRow = null;
let currentRow: { code: string; slot?: string; startLine?: number; endLine?: number } | null = null;
for (const line of rawLines) {
const startMatch = /__SLOT_BEGIN_(\w+)/.exec(line);
if (startMatch) {
Expand Down Expand Up @@ -58,30 +58,30 @@ const html = await codeToHtml(code, {
// Turn it into a DOM tree we can query
const parsed = new JSDOM(html)
const dom = parsed.window.document
const preTag = dom.querySelector('pre')
const codeTag = dom.querySelector('code')
const preTag = dom.querySelector('pre')!
const codeTag = dom.querySelector('code')!
// Extract code lines
const lines = [...dom.querySelectorAll('span.line')]
// Extract the style attributes on the code/pre tags (we'll manually add these back)
const preAttrs = {};
const preAttrs: Record<string, string> = {};
for (const attr of preTag.attributes) {
if (attr.name === 'style') continue
preAttrs[attr.name] = attr.value;
}
const preStyle = preTag.attributes.style.value;
const codeAttrs = {};
const preStyle = preTag.getAttribute('style');
const codeAttrs: Record<string, string> = {};
for (const attr of codeTag.attributes) {
codeAttrs[attr.name] = attr.value;
}
---
<table class="h-1">
<tbody>
{rows.map((row, i) => {
{rows.map(row => {
const lineNodes = lines.slice(row.startLine, row.endLine);
return (
<tr key={i}>
<tr>
<td class="align-top" style="height:100%; margin:0">
<pre {...preAttrs} style={`${preStyle};height:100%`}><code {...codeAttrs} set:html={lineNodes.map((n) => n.outerHTML).join('\n')} /></pre>
</td>
Expand Down

0 comments on commit 888fbe1

Please sign in to comment.