Skip to content

Commit

Permalink
Fixed plugin for application link processing
Browse files Browse the repository at this point in the history
  • Loading branch information
felder101 committed Jun 4, 2024
1 parent 58c547b commit 80bc62f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
9 changes: 5 additions & 4 deletions training-front-end/astro.config.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { defineConfig } from 'astro/config';
import {remarkHeadingId} from 'remark-custom-heading-id';
import { remarkHeadingId } from 'remark-custom-heading-id';
import mdx from '@astrojs/mdx';
import vue from "@astrojs/vue";
import uswds_links from "./src/plugins/uswds_links";
import { processLinksPlugin } from "./src/plugins/uswds_links";

import sitemap from "@astrojs/sitemap";

Expand All @@ -14,6 +14,7 @@ export default defineConfig({
outDir: '../_site',
markdown: {
remarkPlugins: [remarkHeadingId],
rehypePlugins: [uswds_links]
}
rehypePlugins: [processLinksPlugin]
},
trailingSlash: 'always',
});
41 changes: 21 additions & 20 deletions training-front-end/src/plugins/uswds_links.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,27 @@
* The purpose of this is to apply the appropriate class to links
* it will add uswds usa-link--external for non gsa.gov links
*/
import selectAll from 'hast-util-select';
import {selectAll} from 'hast-util-select';

export default () => {
return node => selectAll('a', node).forEach(node => {
const properties = node.properties
properties.className = 'usa-link'
export function processLinksPlugin() {
return function (node) {
selectAll('a', node).forEach(node => {
const properties = node.properties
properties.className = 'usa-link'

let domain;
try {
domain = new URL(properties.href)
} catch(e) {
// Exceptions will be raised with relative links
// these will all be local, so ignore
return
}

if (! domain.hostname.endsWith('gsa.gov')) {
properties.className += ' usa-link--external'
}

});
};
let domain;
try {
domain = new URL(properties.href)
} catch(e) {
// Exceptions will be raised with relative links
// these will all be local, so ignore
return
}

if (! domain.hostname.endsWith('gsa.gov')) {
properties.className += ' usa-link--external'
}

});
}
}

0 comments on commit 80bc62f

Please sign in to comment.