Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add relative path support for transforming urls #52

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
cda52f5
chore: move test fixtures to content directory
techfg Apr 21, 2024
206af52
chore: update slugs relative to collection dir
techfg Apr 21, 2024
ec6a80b
fix: type import
techfg Apr 21, 2024
e3c9ac1
fix: test name
techfg Apr 21, 2024
d8e88f2
chore: update tests for removal of contentPath option
techfg Apr 21, 2024
fd42a8d
fix: replace contentPath option with srcDir option
techfg Apr 21, 2024
8522870
chore: add tests & types
techfg Apr 22, 2024
329af33
fix: apply collection base handling on a per collection basis
techfg Apr 22, 2024
a60ba3e
chore: add tests
techfg Apr 22, 2024
7a24c29
feat: add ability to specify content collection name override (#24)
techfg Apr 22, 2024
fa5cc35
perf: cache frontmatter data
techfg Apr 23, 2024
256eee2
chore: improve test coverage, names & structure
techfg Apr 24, 2024
25b1d9f
chore: refactor resolving collection option overrides
techfg Apr 24, 2024
8f0b73c
chore: disable cache during test
techfg Apr 24, 2024
8592fdb
chore: add missing test
techfg Apr 24, 2024
8bb063a
feat: add collectionRelative support for transforming urls
techfg Apr 25, 2024
34915aa
feat: add pathRelative support for transforming urls
techfg Apr 25, 2024
96296a2
fix: file paths in tests for windows
techfg Apr 25, 2024
9da9037
chore: namespace env variable
techfg Apr 25, 2024
70a8f20
fix: test configuration
techfg Apr 28, 2024
00e7f5f
fix: relative paths to index pages
techfg Apr 28, 2024
d0d2f01
fix: test on windows
techfg Apr 28, 2024
1cd555f
fix: file paths on windows
techfg Apr 29, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ astro-rehype-relative-markdown-links

### Interfaces

- [CollectionConfig](interfaces/CollectionConfig.md)
- [Options](interfaces/Options.md)

### Functions
Expand Down Expand Up @@ -37,4 +38,4 @@ Rehype plugin for Astro to add support for transforming relative links in MD and

#### Defined in

[src/index.mjs:33](https://github.com/vernak2539/astro-rehype-relative-markdown-links/blob/main/src/index.mjs#L33)
[src/index.mjs:36](https://github.com/vernak2539/astro-rehype-relative-markdown-links/blob/main/src/index.mjs#L36)
64 changes: 64 additions & 0 deletions docs/interfaces/CollectionConfig.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
[astro-rehype-relative-markdown-links](../README.md) / CollectionConfig

# Interface: CollectionConfig

## Hierarchy

- `input`\<`CollectionConfigSchemaType`\>

↳ **`CollectionConfig`**

## Table of contents

### Properties

- [base](CollectionConfig.md#base)
- [name](CollectionConfig.md#name)

## Properties

### base

• `Optional` **base**: ``false`` \| ``"name"`` \| ``"collectionRelative"`` \| ``"pathRelative"``

**`Name`**

base

**`Description`**

Override the top-level [collectionBase](Options.md#collectionbase) option for this collection.

#### Inherited from

z.input.base

#### Defined in

[src/options.mjs:17](https://github.com/vernak2539/astro-rehype-relative-markdown-links/blob/main/src/options.mjs#L17)

___

### name

• `Optional` **name**: `string`

**`Name`**

name

**`Description`**

Override the name of the collection from disk.

Use this option when your collection page path does not correspond to the name of the collection on disk (ex. `src/content/docs/reference.md` resolves to a page path of /my-docs/reference).

When not specified, the name of the collection from disk will be used where applicable.

#### Inherited from

z.input.name

#### Defined in

[src/options.mjs:28](https://github.com/vernak2539/astro-rehype-relative-markdown-links/blob/main/src/options.mjs#L28)
152 changes: 119 additions & 33 deletions docs/interfaces/Options.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
### Properties

- [basePath](Options.md#basepath)
- [collectionPathMode](Options.md#collectionpathmode)
- [contentPath](Options.md#contentpath)
- [collectionBase](Options.md#collectionbase)
- [collections](Options.md#collections)
- [srcDir](Options.md#srcdir)
- [trailingSlash](Options.md#trailingslash)

## Properties
Expand All @@ -35,11 +36,11 @@ https://docs.astro.build/en/reference/configuration-reference/#base

The base path to deploy to. Astro will use this path as the root for your pages and assets both in development and in production build.

In the example below, `astro dev` will start your server at `/docs`.
In the example below, `astro dev` will start your server at `/my-site`.

```js
{
base: '/docs'
base: '/my-site'
}
```

Expand All @@ -49,74 +50,159 @@ z.input.basePath

#### Defined in

[src/options.mjs:50](https://github.com/vernak2539/astro-rehype-relative-markdown-links/blob/main/src/options.mjs#L50)
[src/options.mjs:131](https://github.com/vernak2539/astro-rehype-relative-markdown-links/blob/main/src/options.mjs#L131)

___

### collectionPathMode
### collectionBase

• `Optional` **collectionPathMode**: ``"root"`` \| ``"subdirectory"``
• `Optional` **collectionBase**: ``false`` \| ``"name"`` \| ``"collectionRelative"`` \| ``"pathRelative"``

**`Name`**

collectionPathMode
collectionBase

**`Default`**

`subdirectory`
`"name"`

**`Description`**

Where you store your collections:
- `subdirectory` - Subdirectories under `contentPath` (ex: `src/content/docs/index.md` where `docs` is the content collection subdirectory of the contentPath `src/content`)
- `root` - Directly inside `contentPath` (ex: `src/content/docs/index.md` where `src/content/docs` is the `contentPath`)
Set how the URL path to the referenced markdown file should be derived:
- `"name"` - An absolute path prefixed with the optional [basePath](Options.md#basepath) followed by the collection name
- `false` - An absolute path prefixed with the optional [basePath](Options.md#basepath)
- `"collectionRelative"` - A relative path from the collection directory
- `"pathRelative"` - A relative path from the current page path

For example, given a file `./guides/section/my-guide.md` referenced from `./guides/section/my-other-guide.md` with
the link `[My Guide](./my-guide.md)` in the content collection `docs`, the transformed url would be:
- `"name"`: `[/basePath]/docs/guides/section/my-guide`
- `false`: `[/basePath]/guides/section/my-guide`
- `"collectionRelative"`: `../../guides/section/my-guide`
- `"pathRelative"`: `my-guide`

Use `false`, `"collectionRelative"`, or `"pathRelative"` when you are treating your content collection as if it were located in the site
root (ex: `src/content/docs/test.md` resolves to the page path `/test` instead of the typical `/docs/test`).

Use `"collectionRelative"` or `"pathRelative"` when you are serving your content collection pages from multiple page path roots that use a
common content collection (ex: `/my-blog/test` and `/your-blog/test` both point to the file `./src/content/posts/test.md`
in the content collection `posts`).

Important Notes:
- This is a top-level option and will apply to all content collections. If you have multiple content collections
and want the behavior to be different on a per content collection basis, add the collection(s) to the [collections](Options.md#collections)
option and provide a value for collection specific [base](CollectionConfig.md) option.
- When using either `"collectionRelative"` or `"pathRelative"`, due to the nature of relative links, you MUST ensure
that any directory paths in your site (e.g., urls to `index` pages), contain a trailing slash. For example, given
`./src/content/docs/index.md`, the url should be `/docs/` and not `/docs` as any link generated on that page by the plugin
for a page inside of `./src/content/docs` directory will not navigate correctly since, in relative terms, `/docs` is
different than `/docs/`. Along this line, it is highly encouraged to apply `trailingSlash="always"` to your Astro site and
this plugin to help avoid relative pathing issues.

Use the `root` configuration option when you are explicitly setting the [contentPath](Options.md#contentpath) property to something other than `src/content` and you want the directory you specify
for [contentPath](Options.md#contentpath) to be treated a single content collection as if it where located in the site root. In most scenarios, you should set this value to `subdirectory` or not
set this value and the default of `subdirectory` will be used.
**`Example`**

```js
{
// Do not apply a collection name segment to the generated absolute URL path
collectionBase: false
}
```

**`See`**

[collections](Options.md#collections)

#### Inherited from

z.input.collectionBase

#### Defined in

[src/options.mjs:94](https://github.com/vernak2539/astro-rehype-relative-markdown-links/blob/main/src/options.mjs#L94)

___

### collections

• `Optional` **collections**: `Record`\<`string`, \{ `base?`: ``false`` \| ``"name"`` \| ``"collectionRelative"`` \| ``"pathRelative"`` ; `name?`: `string` }\>

**`Name`**

collections

**`Default`**

`{}`

**`Description`**

Specify a mapping of collections where the `key` is the name of a collection on disk and the value is a [CollectionConfig](CollectionConfig.md)
which will override any top-level configuration where applicable.

**`Example`**

```js
{
// Use 'subdirectory' mode
collectionPathMode: 'subdirectory'
// Do not apply a collection name segment to the generated absolute URL path for the collection `docs`
collections: {
docs: {
base: false
}
}
}
```

**`See`**

[CollectionConfig](CollectionConfig.md)

#### Inherited from

z.input.collectionPathMode
z.input.collections

#### Defined in

[src/options.mjs:33](https://github.com/vernak2539/astro-rehype-relative-markdown-links/blob/main/src/options.mjs#L33)
[src/options.mjs:116](https://github.com/vernak2539/astro-rehype-relative-markdown-links/blob/main/src/options.mjs#L116)

___

### contentPath
### srcDir

• `Optional` **contentPath**: `string`
• `Optional` **srcDir**: `string`

**`Name`**

contentPath
srcDir

**`Reference`**

https://docs.astro.build/en/reference/configuration-reference/#srcdir

**`Default`**

`src/content`
`./src`

**`Description`**

This defines where the content (i.e. md, mdx, etc. files) is stored. This should be a path relative to the root directory
Set the directory that Astro will read your site from.

The value can be either an absolute file system path or a path relative to the project root.

**`Example`**

```js
{
srcDir: './www'
}
```

#### Inherited from

z.input.contentPath
z.input.srcDir

#### Defined in

[src/options.mjs:12](https://github.com/vernak2539/astro-rehype-relative-markdown-links/blob/main/src/options.mjs#L12)
[src/options.mjs:49](https://github.com/vernak2539/astro-rehype-relative-markdown-links/blob/main/src/options.mjs#L49)

___

Expand All @@ -130,16 +216,16 @@ trailingSlash

**`Default`**

`ignore`
`"ignore"`

**`Description`**

Allows you to control the behavior for how trailing slashes should be handled on transformed urls:
- `'always'` - Ensure urls always end with a trailing slash regardless of input
- `'never'` - Ensure urls never end with a trailing slash regardless of input
- `'ignore'` - Do not modify the url, trailing slash behavior will be determined by the file url itself or a custom slug if present.
- `"always"` - Ensure urls always end with a trailing slash regardless of input
- `"never"` - Ensure urls never end with a trailing slash regardless of input
- `"ignore"` - Do not modify the url, trailing slash behavior will be determined by the file url itself or a custom slug if present.

When set to `'ignore'` (the default), the following will occur:
When set to `"ignore"` (the default), the following will occur:
- If there is not a custom slug on the target file, the markdown link itself will determine if there is a trailing slash.
- `[Example](./my-doc.md/)` will result in a trailing slash
- `[Example](./my-doc.md)` will not result in a trailing slash
Expand All @@ -152,7 +238,7 @@ When set to `'ignore'` (the default), the following will occur:
```js
{
// Use `always` mode
trailingSlash: `always`
trailingSlash: "always"
}
```

Expand All @@ -166,4 +252,4 @@ z.input.trailingSlash

#### Defined in

[src/options.mjs:77](https://github.com/vernak2539/astro-rehype-relative-markdown-links/blob/main/src/options.mjs#L77)
[src/options.mjs:158](https://github.com/vernak2539/astro-rehype-relative-markdown-links/blob/main/src/options.mjs#L158)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"pre-release": "yarn run changelog && yarn run prettier && yarn run generate-docs",
"generate-docs": "typedoc --readme none --gitRevision main --plugin typedoc-plugin-markdown src",
"prettier": "prettier ./src/** -w",
"test": "node --loader=esmock --test",
"test": "ARRML_MATTER_CACHE_DISABLE=true node --loader=esmock --test",
"type-check": "tsc --noEmit --emitDeclarationOnly false"
},
"dependencies": {
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
slug: slug-with-trailing-slash/
---
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
slug: slug-without-trailing-slash
---
3 changes: 3 additions & 0 deletions src/fixtures/content/docs/_test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Test

Test [link](./other-markdown.md)
1 change: 1 addition & 0 deletions src/fixtures/content/docs/dir-test/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## Test
File renamed without changes.
3 changes: 3 additions & 0 deletions src/fixtures/content/docs/test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Test

Test [link](./other-markdown.md)
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
slug: test.custom.slug.is.collection.root
---

Test
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
slug: dir-child-1/dir-grandchild-1/test.custom.slug
---

Test
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Test

Test [link](./other-markdown.md)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Test

Test [link](./other-markdown.md)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Test

Test [link](./other-markdown.md)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
slug: dir-child-1/test.custom.slug.with-forward.slash/
---

Test
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Test

Test [link](./other-markdown.md)
3 changes: 3 additions & 0 deletions src/fixtures/content/relative-tests/dir-child-1/test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Test

Test [link](./other-markdown.md)
3 changes: 3 additions & 0 deletions src/fixtures/content/relative-tests/dir-child-2/test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Test

Test [link](./other-markdown.md)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Test

Test [link](./other-markdown.md)
Loading