feat: support custom starting line number in code blocks (#287)#407
Open
sleitor wants to merge 1 commit intovercel:mainfrom
Open
feat: support custom starting line number in code blocks (#287)#407sleitor wants to merge 1 commit intovercel:mainfrom
sleitor wants to merge 1 commit intovercel:mainfrom
Conversation
Add support for specifying a custom starting line number via the code fence meta string, e.g.: ```js startLine=10 const x = 1; ``` Implementation: - Add `remarkCodeMeta` remark plugin that forwards the fenced-code meta string to hast as the `metastring` property so it is available to the custom React code component. - Update the rehype-sanitize default schema to allow the `metastring` attribute on `code` elements. - Parse `startLine=N` from the meta string in `CodeComponent` and pass the value down as a `startLine` prop. - In `CodeBlockBody`, apply `counter-reset: line N-1` as an inline style when `startLine > 1`, which overrides the Tailwind counter-reset class and makes CSS counters begin from the specified number. - Pass `startLine` through the `CodeBlock` and `HighlightedCodeBlockBody` call chains. - Add 12 tests covering the remark plugin, CodeBlockBody prop, and CodeBlock integration. Closes: resolves vercel#287
Contributor
|
Someone is attempting to deploy a commit to the Vercel Team on Vercel. A member of the Team first needs to authorize it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements the feature requested in #287: support for custom starting line numbers in fenced code blocks via a
startLine=Nmeta option.Thanks to @haydenbleasel for the CSS workaround hint in the issue — this PR formalises it as a first-class feature!
Usage
Line numbers will begin at 10 instead of the default 1.
How it works
remarkCodeMetaremark plugin (lib/remark/code-meta.ts) — forwards the fenced-code meta string to the hastcodeelement as ametastringproperty, so it is available to the custom React code component.rehype-sanitizeschema is updated to allow themetastringattribute oncodeelements.CodeComponent(lib/components.tsx) — parsesstartLine=Nfrommetastringand passes it as astartLineprop toCodeBlock.CodeBlockBody(lib/code-block/body.tsx) — appliescounter-reset: line N-1as an inline style whenstartLine > 1. This overrides the Tailwind[counter-reset:line]class and makes CSS counters begin from the specified number.CodeBlock/HighlightedCodeBlockBody— thestartLineprop is threaded through the component tree.Changes
packages/streamdown/lib/remark/code-meta.ts— new remark pluginpackages/streamdown/index.tsx— import plugin, update sanitize schema, add todefaultRemarkPluginspackages/streamdown/lib/components.tsx— parsestartLinefrom meta, pass toCodeBlockpackages/streamdown/lib/code-block/index.tsx— addstartLineproppackages/streamdown/lib/code-block/highlighted-body.tsx— passstartLinethroughpackages/streamdown/lib/code-block/body.tsx— applycounter-resetstylepackages/streamdown/__tests__/code-block-start-line.test.tsx— 12 new tests.changeset/code-block-start-line.md— changesetTests
12 new tests covering:
remarkCodeMetaunit tests (plugin attaches/omitsmetastringcorrectly)CodeBlockBodywithstartLineprop (correct CSS counter-reset value)CodeBlockintegration (renders with correct CSS for variousstartLinevalues)All 804 tests pass.