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

feat: allow importing code surrounded by a token #102

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const MIN_LENGTH = 9;
const START_CODES = [64, 91, 99, 111, 100, 101];

// Regexp to match the import syntax.
const SYNTAX_RE = /^@\[code(?:{(?:(\d+)?-(\d+)?)})?(?: ([^\]]+))?\]\(([^)]*)\)/;
const SYNTAX_RE = /^@\[code(?:{(?:(\d+|[\w-]+)?-?(\d+)?)})?(?: ([^\]]+))?\]\(([^)]*)\)/;

export const createImportCodeBlockRule =
(): RuleBlock =>
Expand Down Expand Up @@ -45,7 +45,7 @@ export const createImportCodeBlockRule =

const meta: ImportCodeTokenMeta = {
importPath,
lineStart: lineStart ? Number.parseInt(lineStart, 10) : 0,
lineStart: lineStart ?? 0,
lineEnd: lineEnd ? Number.parseInt(lineEnd, 10) : undefined,
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export type ImportCodeTokenMeta = {
importPath: string;
lineStart: number;
lineStart: number | string;
lineEnd?: number;
};
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,24 @@ export const resolveImportCode = (
// Read file content.
const fileContent = fs.readFileSync(importFilePath).toString();

let importCode = fileContent.split('\n');

if (!Number.isNaN(Number(lineStart))) {
lineStart = Number(lineStart);
importCode = importCode.slice(lineStart ? lineStart - 1 : lineStart, lineEnd);
} else if (typeof lineStart === 'string') {
importCode = importCode.slice(
1 + importCode.findIndex((line) => line.includes(lineStart as string)),
);
let end = importCode.findIndex((line) => line.includes(lineStart as string));
if (end > -1) {
importCode = importCode.slice(0, end - 1);
}
}

// Resolve partial import.
return {
importFilePath,
importCode: fileContent
.split('\n')
.slice(lineStart ? lineStart - 1 : lineStart, lineEnd)
.join('\n')
.replace(/\n?$/, '\n'),
importCode: importCode.join('\n').replace(/\n?$/, '\n'),
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ You should NOT do this.

## Steps

````md copy
`````md copy
:::steps

!!!step title="Step 1"|description="This is a description for step 1."
Expand All @@ -191,7 +191,6 @@ const step = 2;
!!!

:::
````

:::steps

Expand Down Expand Up @@ -228,6 +227,7 @@ const step = 1;

:::
````
`````

:::steps

Expand Down Expand Up @@ -446,12 +446,33 @@ You can import code blocks from files with the following syntax:
@[code](../foo.js)
```

If you want to partially import the file:
If you want to partially import the file, you can do it via line numbers:

```md
@[code{1-10}](../foo.js)
```

Or via a token that surrounds the desired code snippet:

```md
@[code{snippet-1}](../foo.js)
```

```js title=foo.js

willNotShow();

// start of snippet-1

if(...) {
doSomething();
}

// end of snippet-1

thisWontShowEither();
```

The code language is inferred from the file extension, however, you can specify it like so:

```md
Expand All @@ -463,4 +484,4 @@ supports all the syntax mentioned above:

```md
@[code js|title=file.js|copy{2,4-5}](../foo.js)
```
```