Skip to content

Commit

Permalink
inject-link-classes into rehype
Browse files Browse the repository at this point in the history
  • Loading branch information
viperehonchuk committed Feb 20, 2024
1 parent 782fdb6 commit 2b05b1d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 76 deletions.
2 changes: 2 additions & 0 deletions src/plugins/rehype/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import rehypeRaw from 'rehype-raw';
import { checkReferencedAnchorsPlugin } from './check-referenced-anchors.ts';
import { injectDtIdsPlugin } from './inject-dt-ids.ts';
import { injectHeadingSlugsPlugin } from './inject-heading-slugs.ts';
import { injectLinkClassesPlugin } from './inject-link-classes.ts';

const rehypePlugins: RehypePlugins = [
injectLinkClassesPlugin,
injectHeadingSlugsPlugin,
[rehypeAutolinkHeadings, { behavior: 'append' }],
rehypeRaw,
Expand Down
50 changes: 50 additions & 0 deletions src/plugins/rehype/inject-link-classes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import type { Element } from 'hast';
import type { Root } from 'hast';
import { visit } from 'unist-util-visit';

import hasPage from '../registry/has-page.js';
import { initRegistry } from '../registry/registry.js';
import getSlugFromUrl from '../utils/get-slug-from-url.js';

const EXTERNAL_LINK_CLASS = 'wd-external';
const MISSING_LINK_CLASS = 'wd-nav-link-not-translated';

function getClassesByUrl(url: string): string[] {
if (url.startsWith('#')) {
return [];
}
if (url.startsWith('http')) {
return [EXTERNAL_LINK_CLASS];
}
const slug = getSlugFromUrl(url);
if (slug === '') {
// Link to the root
return [];
}
const isPresent = hasPage(slug);
if (!isPresent) {
return [MISSING_LINK_CLASS];
}
return [];
}

export default async function injectLinkClasses(tree: Root) {
await initRegistry();

visit(tree, 'element', (node: Element) => {
if (node.tagName !== 'a') return;
const url = node.properties?.href;
if (typeof url !== 'string') return;
const classes = getClassesByUrl(url);
node.properties.class = classes.join(' ');
if (classes.includes(EXTERNAL_LINK_CLASS)) {
node.properties.target = '_blank';
node.properties.rel = 'noopener noreferrer';
node.properties['data-astro-prefetch'] = 'false';
}
});
}

export function injectLinkClassesPlugin() {
return injectLinkClasses;
}
2 changes: 0 additions & 2 deletions src/plugins/remark/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ import { extractSectionPlugin } from './extract-section.ts';
import { fixLocalImagesPlugin } from './fix-local-images.ts';
import { fixNolintLanguagePlugin } from './fix-nolint-language.ts';
import injectHistoryPlugin from './inject-history/index.ts';
import { injectLinkClassesPlugin } from './inject-link-classes.ts';
import expandMacrosPlugin from './macros/plugin.ts';
const remarkPlugins: RemarkPlugins = [
// headings,
// remarkFrontmatter,
extractSectionPlugin,
extractDescriptionPlugin,
expandMacrosPlugin,
injectLinkClassesPlugin,
fixLocalImagesPlugin,
extractCoverPlugin,
fixNolintLanguagePlugin,
Expand Down
74 changes: 0 additions & 74 deletions src/plugins/remark/inject-link-classes.ts

This file was deleted.

0 comments on commit 2b05b1d

Please sign in to comment.