GitbookIO / GitbookIO/integrations

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

Open
#822 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
131
Forks
96
Avg merge
7h 8m
Merged PRs (30d)
31

Description

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:

https://github.com/GitbookIO/integrations/blob/cb7ab97ddfd63c1ca10c654cc82f31e77c8e4a39/integrations/github-files/src/github.ts#L8-L12

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in integrations/github-files/src/github.ts at the permalinkRegex and wholeFileRegex around lines 8-12. Update the repository-name matching behavior and verify that GitHub file URLs for repositories containing dots, such as vercel/next.js, are captured and unfurled instead of displaying Not Found.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
backend
Issue type
Bug
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.