File tree Expand file tree Collapse file tree 2 files changed +20
-0
lines changed Expand file tree Collapse file tree 2 files changed +20
-0
lines changed Original file line number Diff line number Diff line change 1+ import { existsSync } from "fs" ;
2+ import { visit } from "unist-util-visit" ;
3+ import url from "url" ;
4+
5+
6+ const rehypeLinkTransformer = ( ) => ( tree , vfile ) => {
7+ visit ( tree , "element" , ( node ) => {
8+ if ( node . tagName === "a" ) {
9+ const href = url . parse ( node . properties . href ) ;
10+ if ( href . hostname === null && href . pathname ?. endsWith ( ".md" ) && existsSync ( vfile . history [ 0 ] ) ) {
11+ href . pathname = href . pathname . replace ( / .m d $ / , ".html" ) ;
12+ node . properties . href = url . format ( href ) ;
13+ }
14+ }
15+ } ) ;
16+ } ;
17+
18+ export default rehypeLinkTransformer ;
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ import remarkRehype from "remark-rehype";
66import rehypeStringify from "rehype-stringify" ;
77import rehypeDocument from "rehype-document" ;
88import specsPreset from "./.remarkrc-specs.js" ;
9+ import rehypeLinkTransformer from "../remark/rehype-link-transformer.js" ;
910
1011
1112dotenv . config ( ) ;
@@ -15,6 +16,7 @@ export default {
1516 specsPreset ,
1617 remarkTorchLight ,
1718 remarkRehype ,
19+ rehypeLinkTransformer ,
1820 [ rehypeDocument , {
1921 css : [ "https://cdn.jsdelivr.net/npm/water.css@2/out/dark.css" ] ,
2022 style : readFileSync ( resolve ( import . meta. dirname , "spec.css" ) , "utf8" )
You can’t perform that action at this time.
0 commit comments