Skip to content

Commit

Permalink
fix: migrate plugin to native typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
techfg committed Dec 11, 2024
1 parent bc56fbf commit 9bead1d
Show file tree
Hide file tree
Showing 20 changed files with 1,600 additions and 460 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ jobs:
run: rm -rf docs
- name: Install Dependencies
run: yarn --immutable
- name: Build
run: yarn build
- name: Publish
run: yarn npm publish
env:
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ oldtest.mjs
!.yarn/releases
!.yarn/plugins
.pnp.*
tsconfig.tsbuildinfo
dist/
12 changes: 12 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"nodejs-testing.extensions": [
{
"extensions": ["mjs", "cjs", "js"],
"parameters": []
},
{
"extensions": ["mts", "cts", "ts"],
"parameters": ["--import", "tsx"]
}
]
}
2 changes: 1 addition & 1 deletion docs/functions/default.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ Rehype plugin for Astro to add support for transforming relative links in MD and

## Defined in

[src/index.d.ts:11](https://github.com/techfg/astro-rehype-relative-markdown-links/blob/main/src/index.d.ts#L11)
[src/index.ts:34](https://github.com/techfg/astro-rehype-relative-markdown-links/blob/main/src/index.ts#L34)
5 changes: 3 additions & 2 deletions docs/interfaces/CollectionConfig.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ Collection specific options
> `optional` **base**: `false` | `"name"`
Override the top-level [collectionBase](Options.md#collectionbase) option for this collection.
base

#### Inherited from

`z.input.base`

#### Defined in

[src/options.mjs:9](https://github.com/techfg/astro-rehype-relative-markdown-links/blob/main/src/options.mjs#L9)
[src/options.ts:10](https://github.com/techfg/astro-rehype-relative-markdown-links/blob/main/src/options.ts#L10)

***

Expand All @@ -49,4 +50,4 @@ When not specified, the name of the collection from disk will be used where appl

#### Defined in

[src/options.mjs:17](https://github.com/techfg/astro-rehype-relative-markdown-links/blob/main/src/options.mjs#L17)
[src/options.ts:18](https://github.com/techfg/astro-rehype-relative-markdown-links/blob/main/src/options.ts#L18)
12 changes: 6 additions & 6 deletions docs/interfaces/Options.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ In the example below, `astro dev` will start your server at `/docs`.

#### Defined in

[src/options.mjs:86](https://github.com/techfg/astro-rehype-relative-markdown-links/blob/main/src/options.mjs#L86)
[src/options.ts:87](https://github.com/techfg/astro-rehype-relative-markdown-links/blob/main/src/options.ts#L87)

***

Expand All @@ -63,7 +63,7 @@ Use `false` when you are treating your content collection as if it were located
set this value and the default of `"name"` will be used.

Note that 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 the [base](CollectionConfig.md#base) property.
the [collections](Options.md#collections) option and provide a value for the base property.

#### Default

Expand All @@ -88,7 +88,7 @@ the [collections](Options.md#collections) option and provide a value for the [ba

#### Defined in

[src/options.mjs:55](https://github.com/techfg/astro-rehype-relative-markdown-links/blob/main/src/options.mjs#L55)
[src/options.ts:56](https://github.com/techfg/astro-rehype-relative-markdown-links/blob/main/src/options.ts#L56)

***

Expand Down Expand Up @@ -126,7 +126,7 @@ configuration where applicable.

#### Defined in

[src/options.mjs:73](https://github.com/techfg/astro-rehype-relative-markdown-links/blob/main/src/options.mjs#L73)
[src/options.ts:74](https://github.com/techfg/astro-rehype-relative-markdown-links/blob/main/src/options.ts#L74)

***

Expand Down Expand Up @@ -160,7 +160,7 @@ The value can be either an absolute file system path or a path relative to the p

#### Defined in

[src/options.mjs:34](https://github.com/techfg/astro-rehype-relative-markdown-links/blob/main/src/options.mjs#L34)
[src/options.ts:35](https://github.com/techfg/astro-rehype-relative-markdown-links/blob/main/src/options.ts#L35)

***

Expand Down Expand Up @@ -206,4 +206,4 @@ When set to `"ignore"` (the default), the following will occur:

#### Defined in

[src/options.mjs:110](https://github.com/techfg/astro-rehype-relative-markdown-links/blob/main/src/options.mjs#L110)
[src/options.ts:111](https://github.com/techfg/astro-rehype-relative-markdown-links/blob/main/src/options.ts#L111)
19 changes: 9 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,12 @@
"relative links"
],
"homepage": "https://github.com/vernak2539/astro-rehype-relative-markdown-links",
"main": "./src/index.mjs",
"main": "./dist/index.js",
"type": "module",
"types": "./src/index.d.ts",
"types": "./dist/index.d.ts",
"files": [
"src/index.mjs",
"src/utils.mjs",
"src/options.mjs",
"src/**/*.d.ts",
"README.md",
"CHANGELOG.md"
"./dist",
"README.md"
],
"repository": {
"type": "git",
Expand All @@ -35,8 +31,9 @@
"pre-release": "yarn run changelog && yarn run prettier && yarn run generate-docs",
"generate-docs": "typedoc",
"prettier": "prettier ./src/** -w",
"test": "ARRML_MATTER_CACHE_DISABLE=true node --loader=esmock --test",
"type-check": "tsc --noEmit --emitDeclarationOnly false"
"test": "ARRML_MATTER_CACHE_DISABLE=true node --loader=esmock --import tsx --test ./src/**/*.test.ts",
"type-check": "tsc --noEmit --emitDeclarationOnly false",
"build": "rimraf dist && tsc --build"
},
"dependencies": {
"catch-unknown": "^2.0.0",
Expand All @@ -58,6 +55,8 @@
"prettier": "^4.0.0-alpha.8",
"rehype": "^13.0.2",
"remark-toc": "^9.0.0",
"rimraf": "^5.0.10",
"tsx": "^4.19.2",
"typedoc": "^0.27.4",
"typedoc-plugin-markdown": "^4.3.2",
"typedoc-plugin-remark": "^1.2.0",
Expand Down
5 changes: 5 additions & 0 deletions src/globals.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
declare namespace NodeJS {
interface ProcessEnv {
ARRML_MATTER_CACHE_DISABLE: string;
}
}
15 changes: 0 additions & 15 deletions src/index.d.ts

This file was deleted.

15 changes: 7 additions & 8 deletions src/index.test.mjs → src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import assert from "node:assert/strict";
import { test, describe } from "node:test";
import { test, describe, TestContext } from "node:test";
import { fileURLToPath, pathToFileURL } from "url";
import path, { dirname } from "path";
import { rehype } from "rehype";
import { visit } from "unist-util-visit";
import esmock from "esmock";
import { validateOptions as validateOptionsOriginal } from "./options.mjs";
import { Options, validateOptions as validateOptionsOriginal } from "./options.ts";

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

import astroRehypeRelativeMarkdownLinks from "./index.mjs";
import astroRehypeRelativeMarkdownLinks from "./index.ts";

/*
NOTE ON ESMOCK USAGE
Expand All @@ -31,8 +31,7 @@ import astroRehypeRelativeMarkdownLinks from "./index.mjs";
- https://github.com/nodejs/node/issues/51164#issuecomment-2034518078
*/

/** @param {Record<string, { currentFilePath?: string }} options */
function testSetupRehype(options = {}) {
function testSetupRehype(options: { currentFilePath?: string } = {}) {
return (tree, file) => {
visit(tree, "element", () => {
const fileInHistory = options.currentFilePath
Expand Down Expand Up @@ -418,10 +417,10 @@ describe("astroRehypeRelativeMarkdownLinks", () => {
});

describe("config option validation", () => {
const runValidationTest = async (context, options) => {
const runValidationTest = async (context: TestContext, options?: Options | null) => {
const validateOptionsMock = context.mock.fn(validateOptionsOriginal);
const astroRehypeRelativeMarkdownLinksMock = await esmock("./index.mjs", {
"./options.mjs": {
const astroRehypeRelativeMarkdownLinksMock = await esmock("./index.ts", {
"./options.ts": {
validateOptions: validateOptionsMock,
},
});
Expand Down
29 changes: 21 additions & 8 deletions src/index.mjs → src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,36 @@ import {
shouldProcessFile,
getMatter,
resolveCollectionBase,
} from "./utils.mjs";
import { validateOptions, mergeCollectionOptions } from "./options.mjs";
} from "./utils.js";
import { validateOptions, mergeCollectionOptions, type Options, type CollectionConfig } from "./options.js";
import type { Plugin } from "unified";
import type { Root } from "hast";

export type { Options, CollectionConfig };

// This package makes a lot of assumptions based on it being used with Astro

const debug = debugFn("astro-rehype-relative-markdown-links");

/**
* @type {import('unified').Plugin<[(import('./options.d.ts').Options | null | undefined)?], import('hast').Root>}
* Rehype plugin for Astro to add support for transforming relative links in MD and MDX files into their final page paths.
* @see {@link Options}
*/
function astroRehypeRelativeMarkdownLinks(opts) {
const astroRehypeRelativeMarkdownLinks: Plugin<[(Options | null | undefined)?], Root> = (opts) => {
const options = validateOptions(opts);

return (tree, file) => {
visit(tree, "element", (node) => {
if (
node.type !== "element" ||
node.tagName !== "a" ||
typeof node.properties.href !== "string" ||
!node.properties.href
typeof node.properties["href"] !== "string" ||
!node.properties["href"]
) {
return;
}

const nodeHref = node.properties.href;
const nodeHref = node.properties["href"];
const [urlPathPart, urlQueryStringAndFragmentPart] =
splitPathFromQueryAndFragment(nodeHref);

Expand All @@ -49,6 +54,10 @@ function astroRehypeRelativeMarkdownLinks(opts) {
}

const currentFile = file.history[0];
if (!currentFile) {
// TODO: Should we log throw here or log an error/warning since its an unexpected state/outcome?
return;
}
const currentFileParsed = path.parse(currentFile);
const currentFileDirectory = currentFileParsed.dir;

Expand Down Expand Up @@ -104,6 +113,10 @@ function astroRehypeRelativeMarkdownLinks(opts) {
const collectionName = path
.dirname(relativeToContentPath)
.split(FILE_PATH_SEPARATOR)[0];
if (!collectionName) {
// TODO: Should we log throw here or log an error/warning since its an unexpected state/outcome?
return;
}
// flatten options merging any collection overrides
const collectionOptions = mergeCollectionOptions(collectionName, options);
if (
Expand Down Expand Up @@ -194,7 +207,7 @@ function astroRehypeRelativeMarkdownLinks(opts) {
debug("URL file resolved slug : %s", resolvedSlug);
debug("Final URL : %s", webPathFinal);

node.properties.href = webPathFinal;
node.properties["href"] = webPathFinal;
});
};
}
Expand Down
21 changes: 0 additions & 21 deletions src/options.d.ts

This file was deleted.

Loading

0 comments on commit 9bead1d

Please sign in to comment.