Skip to content
This repository was archived by the owner on Apr 3, 2024. It is now read-only.

[BUG] handle subdirectories when building relative link URLs #449

@johnemau

Description

@johnemau

What / Why

The problem is described well in the following NPM community post and PR

The package.respository.directory is not honored when generating URLs for relative paths in README.mds of NPM packages displayed on npmjs.org.

How

  • n/a

When

  • n/a

Where

npmjs.org package pages for example, any link on https://www.npmjs.com/package/hint/v/6.0.1#further-reading

Current Behavior

Links 404 because link does not include subproject directory path provided in repository.directory of package.json

Steps to Reproduce

Add a relative link to README.md of a package in a subdirectory of a monorepo and publish that package.

Expected Behavior

Relative links become fully qualified URLs to github subproject resource in monrepo.

Who

  • n/a

The Fix

First the consumer of marky-markdown must be passed a package.json meta-data including the repository.directory, I could not find the source code for npmjs.org but I assume this is not the case today because lib/plugin/github.js treats package.repository as a string type, which does not match the example in the README.md.

- var repo = gh(opts.package.repository)
+ // Handling both `string` and `{ url: string }` types
+ var repo = gh(opts.package.repository.url | opt.package.repository)

https://github.com/npm/marky-markdown/blob/master/lib/plugin/github.js#L52

Next buildLinkUrl should use opt.package.repository.directory when building the fully qualified path:

- return prefix + path.join(repository.user, repository.repo, DEFAULT_REF, url.href)
+ // Just an example, `opt` is not in scope, refactor to pass in the repository directory,
+ return prefix + path.join(repository.user, repository.repo, DEFAULT_REF, opt.package.repository.directory, url.href)

https://github.com/npm/marky-markdown/blob/master/lib/plugin/github.js#L12

References

Related to #435
Related to #446

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