Skip to content

Commit

Permalink
code line
Browse files Browse the repository at this point in the history
  • Loading branch information
nialexsan committed Oct 16, 2024
1 parent 064d79a commit 87e788d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ Example:

````markdown
```cadence DepositFees.cdc
!from https://raw.githubusercontent.com/onflow/flow-core-contracts/refs/heads/master/transactions/FlowServiceAccount/deposit_fees.cdc
!from https://github.com/onflow/flow-core-contracts/blob/master/transactions/FlowServiceAccount/deposit_fees.cdc#L23-L26
```
````

Expand Down
26 changes: 25 additions & 1 deletion src/plugins/code-reference.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@ const getUrl = (nodeValue) => {
return url.replace(githubReplace, '$1raw.githubusercontent.com/$3/$4');
};

const getLines = (url) => {
const lines = url.split('#')[1];
if (!lines) {
return null;
}

const [start, end] = lines
.split('-')
.map((line) => line.replace(/L(\d+)/, '$1'));

return [start, end];
};

const plugin = () => {
const transformer = async (ast) => {
const promises = [];
Expand All @@ -20,6 +33,9 @@ const plugin = () => {
if (!url) {
return;
}

const lines = getLines(url);

const fetchPromise = fetch(url)
.then((res) => {
if (!res.ok) {
Expand All @@ -30,7 +46,15 @@ const plugin = () => {
return res.text();
})
.then((code) => {
node.value = code;
if (lines) {
const codeLines = code
.split('\n')
.slice(lines[0] - 1, lines[1])
.join('\n');
node.value = codeLines;
} else {
node.value = code;
}
})
.catch((err) => {
console.error(err);
Expand Down

0 comments on commit 87e788d

Please sign in to comment.