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: custom starting index for line numbers #19

Merged
merged 3 commits into from Nov 15, 2021
Merged

Conversation

PsyGik
Copy link
Contributor

@PsyGik PsyGik commented Nov 14, 2021

this change allows to start numbering at any index

Usage:

showLineNumbers=65 (here, numbering will start at index 65):

```javascript  showLineNumbers=65
const calculateStartingLine = (meta) => {
  const RE = /showLineNumbers=(?<lines>\d+)/i
  // pick the line number after = using a named capturing group
  if (RE.test(meta)) {
    const {
      groups: { lines },
    } = RE.exec(meta)
    return Number(lines)
  }
  return 1
}
```

will produce:

<code class="language-javascript">
<span class="code-line line-number" line="65"><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token template-punctuation string">`</span></span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">javascript title="rehype.js" showLineNumbers=65
</span></span></span><span class="code-line line-number" line="66"><span class="token template-string"><span class="token string">const calculateStartingLine = (meta) =&gt; {
</span></span></span><span class="code-line line-number" line="67"><span class="token template-string"><span class="token string">  const RE = /showLineNumbers=(?&lt;lines&gt;\d+)/i
</span></span></span><span class="code-line line-number" line="68"><span class="token template-string"><span class="token string">  // pick the line number after = using a named capturing group
</span></span></span><span class="code-line line-number" line="69"><span class="token template-string"><span class="token string">  if (RE.test(meta)) {
</span></span></span><span class="code-line line-number" line="70"><span class="token template-string"><span class="token string">    const {
</span></span></span><span class="code-line line-number" line="71"><span class="token template-string"><span class="token string">      groups: { lines },
</span></span></span><span class="code-line line-number" line="72"><span class="token template-string"><span class="token string">    } = RE.exec(meta)
</span></span></span><span class="code-line line-number" line="73"><span class="token template-string"><span class="token string">    return Number(lines)
</span></span></span><span class="code-line line-number" line="74"><span class="token template-string"><span class="token string">  }
</span></span></span><span class="code-line line-number" line="75"><span class="token template-string"><span class="token string">  return 0
</span></span></span><span class="code-line line-number" line="76"><span class="token template-string"><span class="token string">}
</span></span></span><span class="code-line line-number" line="77"><span class="token template-string"><span class="token string"></span><span class="token template-punctuation string">`</span></span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token template-punctuation string">`</span></span>
</span></code>

which looks like:

image

To start numbering from 1, simply use showLineNumbers or showLineNumbers=true without specifying the starting index

This is inspired from gatsby-remark-prismjs

this change allows to start numbering at any index 
`showLineNumbers=5` (here, numbering will start at index 5):

This is inspired from gatsby-remark-prismjs
@PsyGik PsyGik changed the title feat: show line numbers from a specified number feat: custom starting index for line numbers Nov 14, 2021
PsyGik and others added 2 commits November 14, 2021 21:10
no need for `(startingLineNumber || 1)`, when we can return 1 directly from `calculateStartingLine`
@PsyGik
Copy link
Contributor Author

PsyGik commented Nov 14, 2021

Updated test for showLineNumbers=X.

I feel showLineNumbers=X will mostly be used in meta property as opposed to passing in as options to the plugin. What do you feel about it?

@timlrx
Copy link
Owner

timlrx commented Nov 15, 2021

Thanks, looks good to me. Agree that showLineNumbers=X will be used be the meta property which is similar to the gatsby plugin as well.

@timlrx timlrx merged commit 23af3de into timlrx:main Nov 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants