Yuki Takei пре 2 година
родитељ
комит
3c9d427c7d

+ 18 - 1
apps/app/src/services/renderer/rehype-plugins/relative-links.spec.ts

@@ -10,6 +10,22 @@ import { relativeLinks } from './relative-links';
 
 
 describe('relativeLinks', () => {
 describe('relativeLinks', () => {
 
 
+  test('do nothing when the options does not have pagePath', () => {
+    // setup
+    const processor = unified()
+      .use(parse)
+      .use(remarkRehype)
+      .use(relativeLinks, {});
+
+    // when
+    const mdastTree = processor.parse('[link](/Sandbox)');
+    const hastTree = processor.runSync(mdastTree) as HastNode;
+
+    // then
+    const anchorElement = select('a', hastTree);
+    expect(anchorElement?.properties?.href).toBe('/Sandbox');
+  });
+
   test.concurrent.each`
   test.concurrent.each`
     originalHref
     originalHref
       ${'http://example.com/Sandbox'}
       ${'http://example.com/Sandbox'}
@@ -17,10 +33,11 @@ describe('relativeLinks', () => {
     `('leaves the original href \'$originalHref\' as-is', ({ originalHref }) => {
     `('leaves the original href \'$originalHref\' as-is', ({ originalHref }) => {
 
 
     // setup
     // setup
+    const pagePath = '/foo/bar/baz';
     const processor = unified()
     const processor = unified()
       .use(parse)
       .use(parse)
       .use(remarkRehype)
       .use(remarkRehype)
-      .use(relativeLinks, {});
+      .use(relativeLinks, { pagePath });
 
 
     // when
     // when
     const mdastTree = processor.parse(`[link](${originalHref})`);
     const mdastTree = processor.parse(`[link](${originalHref})`);

+ 4 - 3
apps/app/src/services/renderer/rehype-plugins/relative-links.ts

@@ -1,7 +1,10 @@
+import assert from 'assert';
+
 import { selectAll, type HastNode, type Element } from 'hast-util-select';
 import { selectAll, type HastNode, type Element } from 'hast-util-select';
 import isAbsolute from 'is-absolute-url';
 import isAbsolute from 'is-absolute-url';
 import type { Plugin } from 'unified';
 import type { Plugin } from 'unified';
 
 
+
 export type IAnchorsSelector = (node: HastNode) => Element[];
 export type IAnchorsSelector = (node: HastNode) => Element[];
 export type IUrlResolver = (relativeHref: string, basePath: string) => URL;
 export type IUrlResolver = (relativeHref: string, basePath: string) => URL;
 
 
@@ -43,9 +46,7 @@ export const relativeLinks: Plugin<[RelativeLinksPluginParams]> = (options = {})
     const anchors = anchorsSelector(tree as HastNode);
     const anchors = anchorsSelector(tree as HastNode);
 
 
     anchors.forEach((anchor) => {
     anchors.forEach((anchor) => {
-      if (anchor.properties == null) {
-        return;
-      }
+      assert(anchor.properties != null);
 
 
       const href = anchor.properties.href;
       const href = anchor.properties.href;
       if (href == null || typeof href !== 'string' || isAbsolute(href) || isAnchorLink(href)) {
       if (href == null || typeof href !== 'string' || isAbsolute(href) || isAnchorLink(href)) {