Skip to content

Regex that captures URL in GitHub Files integration doesn't allow for '.' (dot) in repository name #822

@boy-vd

Description

@boy-vd

GitHub allows for the presence of dots in repository names, but the regex to capture and unfurl a URL to a file on GitHub doesn't account for this:

const splitGithubUrl = (url: string) => {
const permalinkRegex =
/^https?:\/\/github\.com\/([\w-]+)\/([\w-]+)\/blob\/([a-f0-9]+)\/(.+?)#(.+)$/;
const wholeFileRegex = /^https?:\/\/github\.com\/([\w-]+)\/([\w-]+)\/blob\/([\w.-]+)\/(.+)$/;
const multipleLineRegex = /^L\d+-L\d+$/;

This results in the GitHub Files integration not working for files in repositories like Next.js: https://github.com/vercel/next.js/ (instead of the contents of the file, the block in GitBook will display 'Not Found').

Solution is to change the regex for the group that captures the repository name slightly, from ([\w-]+) to ([\w.-]+):

const splitGithubUrl = (url: string) => {
    const permalinkRegex =
        /^https?:\/\/github\.com\/([\w-]+)\/([\w.-]+)\/blob\/([a-f0-9]+)\/(.+?)#(.+)$/;
    const wholeFileRegex = /^https?:\/\/github\.com\/([\w-]+)\/([\w.-]+)\/blob\/([\w.-]+)\/(.+)$/;
    const multipleLineRegex = /^L\d+-L\d+$/;

Note that usernames on GitHub don't allow the inclusion of dots, so this change seems to be necessary for repository names only.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions