Skip to content

feat(github-code): block to embed GitHub source with line ranges#1082

Open
markdaugherty wants to merge 7 commits into
mainfrom
feature/github-code-block
Open

feat(github-code): block to embed GitHub source with line ranges#1082
markdaugherty wants to merge 7 commits into
mainfrom
feature/github-code-block

Conversation

@markdaugherty

@markdaugherty markdaugherty commented Jun 24, 2026

Copy link
Copy Markdown
Member

Summary

Adds a new github-code block that embeds source code from a GitHub repository, with an optional line-number range.

  • Author pastes a standard GitHub blob URL (e.g. https://github.com/owner/repo/blob/main/path/file.js#L10-L20) — or a GitHub "Copy permalink" URL with column numbers (#L10C1-L20C5) — or just a bare URL with no link markup; the block parses owner/repo/ref/path and the optional line range.
  • A bare blob URL alone in its own paragraph is also auto-blocked into a github-code block, so authors can just drop the link in without building the block table by hand. Links inside prose, lists, blockquotes, or authored block cells are left untouched.
  • Fetches the file from raw.githubusercontent.com, slicing to the requested lines (whole file if no range), and strips the trailing blank line raw GitHub files end with.
  • Renders into pre > code and highlights via the existing locally-hosted highlight.js (/libs/highlight/, atom-one-dark), loaded lazily on scroll-into-view — mirroring the guide-template code-block pattern.
  • Reuses the shared addCopyButtonsToCodeBlocks helper (now exported from scripts.js) for the copy button.
  • Header shows the file path (percent-decoded), a ref badge (branch/tag, or a truncated 7-char SHA for full commit hashes), the line range, and a "View on GitHub" link.
  • Snippets over 40 lines collapse behind a "Show all N lines" / "Show fewer lines" toggle.
  • Falls back to a styled error message (linking back to GitHub) on an invalid/out-of-range line fragment or a fetch failure, and to a plain "View code on GitHub" link for URLs that aren't a recognizable GitHub blob URL.

Preview

https://feature-github-code-block--helix-website--adobe.aem.page/drafts/mdaugherty/github-code-block

Notes

  • The bundled highlight.js build now registers bash, css, javascript, ruby, yaml, xml (html/svg), json — xml and json grammars were appended from the official 11.7.0 cdnjs build so HTML and JSON snippets get real token coloring instead of just the themed background. Other languages (python, typescript, java, etc.) still render in the themed panel without color; the block applies the hljs class itself so the background stays consistent and skips the highlighter when a grammar isn't available (avoids console warnings).
  • addCopyButtonsToCodeBlocks() scans the document and is idempotent (skips pres that already have a button), matching the guide-template behavior.
  • Fixed a race where multiple github-code blocks becoming visible at once could each call the shared loadScript helper independently; since that helper resolves as soon as it sees a matching <script> tag (even one still loading), later blocks could silently skip highlighting. The block now memoizes a single load promise per page.

Testing

  • ✅ Verified every variant (line range, single line, whole file, long-file toggle, permalink with column numbers, commit-SHA ref truncation, invalid line range, invalid URL, autoblocked bare link, prose link left alone, json/xml highlighting) renders correctly with copy buttons and no console errors (Playwright), against local draft content.
  • npm run lint clean for JS and CSS.

🤖 Generated with Claude Code

Mark Daugherty and others added 2 commits June 23, 2026 18:28
Adds a github-code block that fetches a file from a GitHub blob URL
(with optional #L start-end line range) via raw.githubusercontent.com,
renders it in a pre/code block, and highlights it with the existing
locally-hosted highlight.js library (atom-one-dark). Includes a copy
button matching the guide-template pattern and a View on GitHub link.
Adds a drafts test page covering line-range, single-line, and whole-file.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…font

Export addCopyButtonsToCodeBlocks from scripts.js and call it from the
github-code block instead of duplicating the copy-button implementation.
Make the View on GitHub link inherit the header font size so it matches
the file-path label.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@aem-code-sync

aem-code-sync Bot commented Jun 24, 2026

Copy link
Copy Markdown

Hello, I'm the AEM Code Sync Bot and I will run some actions to deploy your branch and validate page speed.
In case there are problems, just click a checkbox below to rerun the respective action.

  • Re-run all PSI checks
  • Re-run failed PSI checks
  • Re-sync branch
Commits

@aem-code-sync

aem-code-sync Bot commented Jun 24, 2026

Copy link
Copy Markdown
Page Scores Audits Google
📱 /drafts/mdaugherty/github-code-block PERFORMANCE A11Y SEO BEST PRACTICES SI FCP LCP TBT CLS PSI
🖥️ /drafts/mdaugherty/github-code-block PERFORMANCE A11Y SEO BEST PRACTICES SI FCP LCP TBT CLS PSI

Mark Daugherty and others added 2 commits July 20, 2026 10:23
Adopts column-number permalink parsing, trailing-newline fix, line-range
validation, path decoding, a ref badge, and a show-more/fewer toggle for
long files from the benpeter/code-block reference, while keeping this
project's rb/jsx language mappings (ruby and javascript are the only
bundled grammars that matter here) and its own highlight.js loading so
copy buttons and highlighting still work standalone. Also fixes a race
where multiple blocks loading highlight.js at once could silently skip
highlighting.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@aem-code-sync
aem-code-sync Bot temporarily deployed to feature/github-code-block July 20, 2026 16:22 Inactive
@aem-code-sync
aem-code-sync Bot temporarily deployed to feature/github-code-block July 20, 2026 16:38 Inactive
benpeter added 2 commits July 20, 2026 21:46
A bare blob URL alone in its paragraph becomes a github-code block, so
docs can drop the file link without authoring a block. The guards keep
it to default-content paragraphs: the link text must equal the href,
the paragraph must hold only that link, and its parent div must sit
directly under main. Links inside prose, lists, blockquotes, or authored
block cells are left alone. The block already reads its URL from the
anchor, so no block change is needed.
The build registered only bash, css, javascript, ruby, and yaml. HTML
and JSON code fell back to no-highlight, so the block renders those
files in the themed panel without token colors. Appended the official
11.7.0 compiled grammar modules from cdnjs so html and json samples get
syntax coloring.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants