Skip to content

GitHub Permalink Expansion v1.0.0~ ​

When you send a link to a file on GitHub (a permalink), babyrite expands the linked file's content as a syntax-highlighted code block.

This feature is gated by a feature flag

It is enabled by default. To disable it, set the corresponding key under [features] in config.toml to false.

toml
[features]
github_permalink = false
md
https://github.com/m1sk9/babyrite/blob/52731e2a66c5647b3bd30b429af87c5e181e3434/src/main.rs#L44-L50

You can copy a permalink with the following steps.

  1. Open the source code on GitHub
  2. Select the starting line
  3. Hold Shift and select the ending line
  4. Click Copy permalink

  • Expands up to 3 links per message.
  • Duplicate URLs are ignored.
  • Each link's expansion is posted separately as a reply to the original message.

How expansions are shown ​

A single line with the file's information is shown above the code block.

md
`src/main.rs` (L44-L50) - m1sk9/babyrite@52731e2
  • The file path, line range and {owner}/{repo}@{ref} are shown in that order.
  • If {ref} is a commit SHA, it is shortened to its first 7 characters. Branch and tag names are shown as-is.
  • Without a line range, the file is shown from the beginning.
  • If the lines to show exceed github.max_lines (50 by default), they are truncated to the first max_lines lines and marked like truncated to 50 lines. The limit can be changed in Configuration.
  • If the line range runs past the end of the file, the lines up to the end are shown.
  • If the lines to show are empty or contain only whitespace (for example an empty file, or a line range that selects only blank lines), the link is not expanded and no reply is posted.

Mentions and special characters

  • Expansion replies mention no one. Even if the file contains @everyone or user mentions, no one is notified.
  • If the file contains ```, it is defused without changing how it looks, so the code block does not close early.

Supported patterns ​

The permalink patterns babyrite supports are:

md
https://github.com/{owner}/{repo}/blob/{ref}/{path}
https://github.com/{owner}/{repo}/blob/{ref}/{path}#L{line}
https://github.com/{owner}/{repo}/blob/{ref}/{path}#L{start}-L{end}
  • {ref} supports the following formats.
    • A commit SHA (4 to 40 hex digits)
    • A branch name (e.g. main or feat/add-github)
    • A tag name (e.g. release-v1.0, v2.9.0)
  • Query strings are discarded.
  • Only public repositories are supported. babyrite fetches files without authentication, so files in private repositories cannot be expanded.

Content fetch limits ​

Content is fetched from https://raw.githubusercontent.com/.

The response body is read incrementally and the transfer is aborted as soon as enough lines for the code block have arrived, so the rest of the file is never downloaded. A file larger than 1MB can therefore still be previewed, as long as the lines that are actually displayed fit within the limit.

If the bytes kept up to the last displayed line exceed 1MB (1_048_576 bytes), the fetch is aborted and an error is returned. The limit is applied to the bytes actually received rather than to Content-Length, because raw.githubusercontent.com may respond with chunked transfer encoding, in which case that header is absent.

For a permalink with a line range (#L{start}-L{end}), the lines before {start} are downloaded only to find where the range begins and are not kept, so they do not count toward the 1MB limit. They are bounded separately: if more than 8MB (8_388_608 bytes) are read before the range begins, the fetch is aborted and an error is returned.

The whole request (connect, response, and body read) is bounded by a 10 second timeout.

Canceling a preview ​

If you don't want a link previewed, wrap it in <> and it will be ignored.

md
<https://github.com/m1sk9/babyrite/blob/52731e2a66c5647b3bd30b429af87c5e181e3434/src/main.rs#L44-L50>

Supported extensions ​

The extensions supported for syntax highlighting are listed below.

Note that Discord code blocks use highlight.js for syntax highlighting, so languages not supported by highlight.js cannot be highlighted regardless of babyrite's configuration.

If syntax highlighting does not work correctly for a supported language, please open an issue.

TIP

For extensions not listed, the extension name is used as-is for the language hint.

About .m

.m is used by both Objective-C and Matlab, but babyrite treats it as Objective-C, which is more common on GitHub. Matlab files are highlighted as Objective-C.

Extension table (click to expand)
ExtensionLanguage
.rsRust
.py, .pyi, .pywPython
.jsJavaScript
.tsTypeScript
.jsxJSX
.tsxTSX
.rbRuby
.goGo
.javaJava
.kt, .ktsKotlin
.c, .hC
.cpp, .cc, .cxx, .hpp, .hxxC++
.csC#
.swiftSwift
.phpPHP
.scalaScala
.sh, .bash, .zsh, .fishBash
.ps1, .psm1PowerShell
.html, .htmHTML
.cssCSS
.scssSCSS
.sassSass
.lessLess
.jsonJSON
.yaml, .ymlYAML
.tomlTOML
.xmlXML
.sqlSQL
.md, .markdownMarkdown
.luaLua
.rR
.dartDart
.zigZig
.nimNim
.ex, .exsElixir
.erl, .hrlErlang
.hsHaskell
.ml, .mliOCaml
.clj, .cljs, .cljcClojure
.tfHCL
.vueVue
.svelteSvelte
.graphql, .gqlGraphQL
.protoProtobuf
.jlJulia
.m, .mmObjective-C
.f, .for, .f77Fortran
.fs, .fsx, .fsiF#
.adb, .adsAda
.asm, .sx86 Assembly
.lisp, .el, .clLisp
.scm, .ss, .rktScheme
.llLLVM IR
.vhdVHDL
.vert, .fragGLSL
.j2Jinja
.confNginx
.htaccess, httpd.conf, apache2.confApache
.styLaTeX
.watWebAssembly
.mk, MakefileMakefile
DockerfileDockerfile
CMakeLists.txtCMake