Skip to content

Commit

Permalink
fix: resolves #18
Browse files Browse the repository at this point in the history
  • Loading branch information
techfg committed Apr 9, 2024
1 parent ead3d02 commit 460421b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
24 changes: 24 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,31 @@
export type TrailingSlash = 'always' | 'never' | 'ignore';

type CollectionPathMode = 'subdirectory' | `root`

export interface Options {
contentPath?: string; // where you store your content relative to the root directory
/**
* @docs
* @name collectionPathMode
* @type {CollectionPathMode}
* @default `'subdirectory'`
* @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/index.md where src/content is the contentPath)
*
* Use the `root` configuration option when you are treating 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.
*
* ```js
* {
* // Example: Use `subdirectory` mode
* collectionPathMode: `subdirectory`
* }
* ```
*/
collectionPathMode?: CollectionPathMode;
basePath?: string; // https://docs.astro.build/en/reference/configuration-reference/#base
/**
* @name trailingSlash
Expand Down
11 changes: 7 additions & 4 deletions src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ const defaultContentPath = ["src", "content"].join(path.sep);
/** @type {import("./index").TrailingSlash} */
const defaultTrailingSlash = 'ignore';

/** @type {import("./index").CollectionPathMode} */
const defaultCollectionPathMode = 'subdirectory';

/** @param {import('./index').Options} options */
function astroRehypeRelativeMarkdownLinks(options = {}) {
return (tree, file) => {
Expand Down Expand Up @@ -57,19 +60,19 @@ function astroRehypeRelativeMarkdownLinks(options = {}) {
const { data: frontmatter } = matter(relativeFileContent);
const frontmatterSlug = frontmatter.slug;
const contentDir = options.contentPath || defaultContentPath;

const collectionPathMode = options.collectionPathMode || defaultCollectionPathMode;
const relativeToContentPath = path.relative(contentDir, relativeFile);
const collectionName = path.dirname(relativeToContentPath).split(path.posix.sep)[0];
const collectionName = collectionPathMode === 'root' ? "" : path.dirname(relativeToContentPath).split(path.posix.sep)[0];
const relativeToCollectionPath = path.relative(collectionName, relativeToContentPath);
const withoutFileExt = replaceExt(relativeToCollectionPath, "")
const pathSegments = withoutFileExt.split(path.posix.sep);
const generatedSlug = generateSlug(pathSegments);
const resolvedSlug = resolveSlug(generatedSlug, frontmatterSlug);
const trailingSlashMode = options.trailingSlash || defaultTrailingSlash;

const resolvedUrl = path.posix.sep +
const resolvedUrl =
[
collectionName,
!collectionName ? '' : (path.posix.sep + collectionName),
resolvedSlug,
].join(path.posix.sep);

Expand Down
14 changes: 7 additions & 7 deletions src/index.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ test("astroRehypeRelativeMarkdownLinks", async (t) => {
const input = '<a href="./fixtures/index.md">foo</a>';
const { value: actual } = await rehype()
.use(testSetupRehype)
.use(astroRehypeRelativeMarkdownLinks, { contentPath: "src/fixtures" })
.use(astroRehypeRelativeMarkdownLinks, { contentPath: "src/fixtures", collectionPathMode: 'root' })
.process(input);

const expected =
Expand All @@ -319,7 +319,7 @@ test("astroRehypeRelativeMarkdownLinks", async (t) => {
const input = '<a href="./fixtures/dir-test-custom-slug/index.md">foo</a>';
const { value: actual } = await rehype()
.use(testSetupRehype)
.use(astroRehypeRelativeMarkdownLinks, { contentPath: "src/fixtures/dir-test-custom-slug" })
.use(astroRehypeRelativeMarkdownLinks, { contentPath: "src/fixtures/dir-test-custom-slug", collectionPathMode: 'root' })
.process(input);

const expected =
Expand All @@ -335,7 +335,7 @@ test("astroRehypeRelativeMarkdownLinks", async (t) => {
const input = '<a href="./fixtures/test.md">foo</a>';
const { value: actual } = await rehype()
.use(testSetupRehype)
.use(astroRehypeRelativeMarkdownLinks, { contentPath: "src/fixtures" })
.use(astroRehypeRelativeMarkdownLinks, { contentPath: "src/fixtures", collectionPathMode: 'root' })
.process(input);

const expected =
Expand All @@ -351,7 +351,7 @@ test("astroRehypeRelativeMarkdownLinks", async (t) => {
const input = '<a href="./fixtures/test-custom-slug.md">foo</a>';
const { value: actual } = await rehype()
.use(testSetupRehype)
.use(astroRehypeRelativeMarkdownLinks, { contentPath: "src/fixtures" })
.use(astroRehypeRelativeMarkdownLinks, { contentPath: "src/fixtures", collectionPathMode: 'root' })
.process(input);

const expected =
Expand All @@ -367,7 +367,7 @@ test("astroRehypeRelativeMarkdownLinks", async (t) => {
const input = '<a href="./fixtures/dir-test/index.md">foo</a>';
const { value: actual } = await rehype()
.use(testSetupRehype)
.use(astroRehypeRelativeMarkdownLinks, { contentPath: "src/fixtures" })
.use(astroRehypeRelativeMarkdownLinks, { contentPath: "src/fixtures", collectionPathMode: 'root' })
.process(input);

const expected =
Expand All @@ -383,7 +383,7 @@ test("astroRehypeRelativeMarkdownLinks", async (t) => {
const input = '<a href="./fixtures/dir-test/dir-test-child.md">foo</a>';
const { value: actual } = await rehype()
.use(testSetupRehype)
.use(astroRehypeRelativeMarkdownLinks, { contentPath: "src/fixtures" })
.use(astroRehypeRelativeMarkdownLinks, { contentPath: "src/fixtures", collectionPathMode: 'root' })
.process(input);

const expected =
Expand All @@ -399,7 +399,7 @@ test("astroRehypeRelativeMarkdownLinks", async (t) => {
const input = '<a href="./fixtures/dir-test-custom-slug.md/test-custom-slug-in-dot-dir.md">foo</a>';
const { value: actual } = await rehype()
.use(testSetupRehype)
.use(astroRehypeRelativeMarkdownLinks, { contentPath: "src/fixtures" })
.use(astroRehypeRelativeMarkdownLinks, { contentPath: "src/fixtures", collectionPathMode: 'root' })
.process(input);

const expected =
Expand Down

0 comments on commit 460421b

Please sign in to comment.