@xsynaptic/satteri-trailing-slash 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +44 -0
- package/dist/index.d.mts +22 -0
- package/dist/index.mjs +43 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +52 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Alexander Synaptic
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# @xsynaptic/satteri-trailing-slash
|
|
2
|
+
|
|
3
|
+
A [Sätteri][] hast plugin that normalizes trailing slashes on internal markdown links to match your
|
|
4
|
+
Astro project's [`trailingSlash`][astro-trailing-slash] setting. Hand-authored links like
|
|
5
|
+
`[text](/path)` emit whatever slash the author typed; when that disagrees with the site's canonical
|
|
6
|
+
form, each link costs a redirect hop. This rewrites them at compile time.
|
|
7
|
+
|
|
8
|
+
Only internal root-relative `<a href>` links are touched, and any `?query`/`#hash` suffix is
|
|
9
|
+
preserved. External, protocol-relative (`//`), hash-only, `mailto:`/`tel:`, and file-extension links
|
|
10
|
+
are left alone, as are MDX components like `<Link>` (they aren't `<a>` nodes at hast time).
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
npm install @xsynaptic/satteri-trailing-slash
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
This package is ESM-only.
|
|
19
|
+
|
|
20
|
+
## Use
|
|
21
|
+
|
|
22
|
+
Pass the same value your `astro.config` uses for `trailingSlash`. The one option, `trailingSlash`,
|
|
23
|
+
takes `'always'` (append a slash), `'never'` (strip it), or `'ignore'` (the default: a no-op, matching
|
|
24
|
+
Astro's own default).
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
import { satteri } from '@astrojs/markdown-satteri';
|
|
28
|
+
import { trailingSlash } from '@xsynaptic/satteri-trailing-slash';
|
|
29
|
+
|
|
30
|
+
export default defineConfig({
|
|
31
|
+
trailingSlash: 'always',
|
|
32
|
+
markdown: {
|
|
33
|
+
processor: satteri({ hastPlugins: [trailingSlash({ trailingSlash: 'always' })] }),
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## License
|
|
39
|
+
|
|
40
|
+
[MIT][mit-license]
|
|
41
|
+
|
|
42
|
+
[Sätteri]: https://github.com/bruits/satteri
|
|
43
|
+
[astro-trailing-slash]: https://docs.astro.build/en/reference/configuration-reference/#trailingslash
|
|
44
|
+
[mit-license]: https://opensource.org/licenses/MIT
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
//#region src/index.d.ts
|
|
4
|
+
declare const optionsSchema: z.ZodObject<{
|
|
5
|
+
trailingSlash: z.ZodDefault<z.ZodEnum<{
|
|
6
|
+
always: "always";
|
|
7
|
+
never: "never";
|
|
8
|
+
ignore: "ignore";
|
|
9
|
+
}>>;
|
|
10
|
+
}, z.core.$strip>;
|
|
11
|
+
type TrailingSlashOptions = z.input<typeof optionsSchema>;
|
|
12
|
+
declare function trailingSlash(options?: null | Readonly<TrailingSlashOptions>): {
|
|
13
|
+
element: {
|
|
14
|
+
filter: string[];
|
|
15
|
+
visit(node: Readonly<import("hast").Element>, ctx: import("satteri").HastVisitorContext): void;
|
|
16
|
+
};
|
|
17
|
+
name: string;
|
|
18
|
+
};
|
|
19
|
+
declare function normalizeHref(href: string, mode: 'always' | 'never'): string | undefined;
|
|
20
|
+
//#endregion
|
|
21
|
+
export { type TrailingSlashOptions, normalizeHref, trailingSlash };
|
|
22
|
+
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { defineHastPlugin } from "satteri";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
//#region src/index.ts
|
|
4
|
+
const optionsSchema = z.object({ trailingSlash: z.enum([
|
|
5
|
+
"always",
|
|
6
|
+
"never",
|
|
7
|
+
"ignore"
|
|
8
|
+
]).default("ignore") });
|
|
9
|
+
function trailingSlash(options) {
|
|
10
|
+
const settings = optionsSchema.parse(options ?? {});
|
|
11
|
+
return defineHastPlugin({
|
|
12
|
+
element: {
|
|
13
|
+
filter: ["a"],
|
|
14
|
+
visit(node, ctx) {
|
|
15
|
+
if (settings.trailingSlash === "ignore") return;
|
|
16
|
+
const href = node.properties.href;
|
|
17
|
+
if (typeof href !== "string") return;
|
|
18
|
+
const next = normalizeHref(href, settings.trailingSlash);
|
|
19
|
+
if (next !== void 0) ctx.setProperty(node, "href", next);
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
name: "trailing-slash"
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
function normalizeHref(href, mode) {
|
|
26
|
+
if (!href.startsWith("/") || href.startsWith("//")) return void 0;
|
|
27
|
+
const suffixMarkers = [href.indexOf("?"), href.indexOf("#")].filter((index) => index !== -1);
|
|
28
|
+
const cut = suffixMarkers.length === 0 ? -1 : Math.min(...suffixMarkers);
|
|
29
|
+
const path = cut === -1 ? href : href.slice(0, cut);
|
|
30
|
+
if (path === "/") return void 0;
|
|
31
|
+
const suffix = cut === -1 ? "" : href.slice(cut);
|
|
32
|
+
if (mode === "always") {
|
|
33
|
+
const lastSegment = path.slice(path.lastIndexOf("/") + 1);
|
|
34
|
+
if (path.endsWith("/") || lastSegment.includes(".")) return void 0;
|
|
35
|
+
return `${path}/${suffix}`;
|
|
36
|
+
}
|
|
37
|
+
if (!path.endsWith("/")) return void 0;
|
|
38
|
+
return `${path.slice(0, -1)}${suffix}`;
|
|
39
|
+
}
|
|
40
|
+
//#endregion
|
|
41
|
+
export { normalizeHref, trailingSlash };
|
|
42
|
+
|
|
43
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["import { defineHastPlugin } from 'satteri';\nimport { z } from 'zod';\n\nconst optionsSchema = z.object({\n\ttrailingSlash: z.enum(['always', 'never', 'ignore']).default('ignore'),\n});\n\ntype TrailingSlashOptions = z.input<typeof optionsSchema>;\n\nexport function trailingSlash(options?: null | Readonly<TrailingSlashOptions>) {\n\tconst settings = optionsSchema.parse(options ?? {});\n\n\treturn defineHastPlugin({\n\t\telement: {\n\t\t\tfilter: ['a'],\n\t\t\tvisit(node, ctx) {\n\t\t\t\tif (settings.trailingSlash === 'ignore') return;\n\n\t\t\t\tconst href = node.properties.href;\n\t\t\t\tif (typeof href !== 'string') return;\n\n\t\t\t\tconst next = normalizeHref(href, settings.trailingSlash);\n\t\t\t\tif (next !== undefined) ctx.setProperty(node, 'href', next);\n\t\t\t},\n\t\t},\n\t\tname: 'trailing-slash',\n\t});\n}\n\n// Returns undefined when nothing should change, so the visitor skips the write\nfunction normalizeHref(href: string, mode: 'always' | 'never'): string | undefined {\n\tif (!href.startsWith('/') || href.startsWith('//')) return undefined; // internal root-relative only\n\n\tconst suffixMarkers = [href.indexOf('?'), href.indexOf('#')].filter((index) => index !== -1);\n\tconst cut = suffixMarkers.length === 0 ? -1 : Math.min(...suffixMarkers);\n\tconst path = cut === -1 ? href : href.slice(0, cut);\n\n\tif (path === '/') return undefined; // root is canonical in both modes\n\n\tconst suffix = cut === -1 ? '' : href.slice(cut);\n\n\tif (mode === 'always') {\n\t\tconst lastSegment = path.slice(path.lastIndexOf('/') + 1);\n\t\tif (path.endsWith('/') || lastSegment.includes('.')) return undefined; // already slashed, or a file\n\t\treturn `${path}/${suffix}`;\n\t}\n\n\tif (!path.endsWith('/')) return undefined; // 'never': nothing to strip\n\treturn `${path.slice(0, -1)}${suffix}`;\n}\n\nexport { normalizeHref };\nexport type { TrailingSlashOptions };\n"],"mappings":";;;AAGA,MAAM,gBAAgB,EAAE,OAAO,EAC9B,eAAe,EAAE,KAAK;CAAC;CAAU;CAAS;AAAQ,CAAC,CAAC,CAAC,QAAQ,QAAQ,EACtE,CAAC;AAID,SAAgB,cAAc,SAAiD;CAC9E,MAAM,WAAW,cAAc,MAAM,WAAW,CAAC,CAAC;CAElD,OAAO,iBAAiB;EACvB,SAAS;GACR,QAAQ,CAAC,GAAG;GACZ,MAAM,MAAM,KAAK;IAChB,IAAI,SAAS,kBAAkB,UAAU;IAEzC,MAAM,OAAO,KAAK,WAAW;IAC7B,IAAI,OAAO,SAAS,UAAU;IAE9B,MAAM,OAAO,cAAc,MAAM,SAAS,aAAa;IACvD,IAAI,SAAS,KAAA,GAAW,IAAI,YAAY,MAAM,QAAQ,IAAI;GAC3D;EACD;EACA,MAAM;CACP,CAAC;AACF;AAGA,SAAS,cAAc,MAAc,MAA8C;CAClF,IAAI,CAAC,KAAK,WAAW,GAAG,KAAK,KAAK,WAAW,IAAI,GAAG,OAAO,KAAA;CAE3D,MAAM,gBAAgB,CAAC,KAAK,QAAQ,GAAG,GAAG,KAAK,QAAQ,GAAG,CAAC,CAAC,CAAC,QAAQ,UAAU,UAAU,EAAE;CAC3F,MAAM,MAAM,cAAc,WAAW,IAAI,KAAK,KAAK,IAAI,GAAG,aAAa;CACvE,MAAM,OAAO,QAAQ,KAAK,OAAO,KAAK,MAAM,GAAG,GAAG;CAElD,IAAI,SAAS,KAAK,OAAO,KAAA;CAEzB,MAAM,SAAS,QAAQ,KAAK,KAAK,KAAK,MAAM,GAAG;CAE/C,IAAI,SAAS,UAAU;EACtB,MAAM,cAAc,KAAK,MAAM,KAAK,YAAY,GAAG,IAAI,CAAC;EACxD,IAAI,KAAK,SAAS,GAAG,KAAK,YAAY,SAAS,GAAG,GAAG,OAAO,KAAA;EAC5D,OAAO,GAAG,KAAK,GAAG;CACnB;CAEA,IAAI,CAAC,KAAK,SAAS,GAAG,GAAG,OAAO,KAAA;CAChC,OAAO,GAAG,KAAK,MAAM,GAAG,EAAE,IAAI;AAC/B"}
|
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@xsynaptic/satteri-trailing-slash",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Sätteri hast plugin that normalizes trailing slashes on internal markdown links to match an Astro project's trailingSlash setting.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "Alexander Synaptic <x@synapticism.com>",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/xsynaptic/astro-lab.git",
|
|
11
|
+
"directory": "packages/satteri-trailing-slash"
|
|
12
|
+
},
|
|
13
|
+
"homepage": "https://github.com/xsynaptic/astro-lab/tree/main/packages/satteri-trailing-slash#readme",
|
|
14
|
+
"bugs": {
|
|
15
|
+
"url": "https://github.com/xsynaptic/astro-lab/issues"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"satteri",
|
|
19
|
+
"satteri-plugin",
|
|
20
|
+
"hast",
|
|
21
|
+
"trailing-slash",
|
|
22
|
+
"astro",
|
|
23
|
+
"seo"
|
|
24
|
+
],
|
|
25
|
+
"main": "./dist/index.mjs",
|
|
26
|
+
"types": "./dist/index.d.mts",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"types": "./dist/index.d.mts",
|
|
30
|
+
"import": "./dist/index.mjs"
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"files": [
|
|
34
|
+
"dist"
|
|
35
|
+
],
|
|
36
|
+
"sideEffects": false,
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"@types/hast": "^3.0.4",
|
|
39
|
+
"zod": "^4.4.3"
|
|
40
|
+
},
|
|
41
|
+
"peerDependencies": {
|
|
42
|
+
"satteri": "^0.9.0"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"satteri": "^0.9.5"
|
|
46
|
+
},
|
|
47
|
+
"scripts": {
|
|
48
|
+
"build": "tsdown",
|
|
49
|
+
"dev": "tsdown --watch",
|
|
50
|
+
"check-types": "tsc --noEmit"
|
|
51
|
+
}
|
|
52
|
+
}
|