diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 7c64117ffb..99eb7264ab 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -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 ``` ```` diff --git a/src/plugins/code-reference.js b/src/plugins/code-reference.js index 779f1f13d7..e1d6dbb820 100644 --- a/src/plugins/code-reference.js +++ b/src/plugins/code-reference.js @@ -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 = []; @@ -20,6 +33,9 @@ const plugin = () => { if (!url) { return; } + + const lines = getLines(url); + const fetchPromise = fetch(url) .then((res) => { if (!res.ok) { @@ -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);