Skip to content

Commit

Permalink
Merge branch 'main' into refactor-engineering-guide-landing-page
Browse files Browse the repository at this point in the history
  • Loading branch information
kbighorse committed Apr 17, 2024
2 parents 9208bbc + f702676 commit fb9afc8
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 28 deletions.
46 changes: 46 additions & 0 deletions .eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,52 @@ module.exports = function (config) {
return openDefaultRender(tokens, idx, options, env, self) + `${prefixIcon}`;
};

const defaultHtmlBlockRender = markdownLibrary.renderer.rules.html_block ||
function(tokens, idx, options, env, self) {
return self.renderToken(tokens, idx, options);
};

markdownLibrary.renderer.rules.html_block = (tokens, idx, options, env, self) => {
const token = tokens[idx];
let content = token.content;
// Capture the class portion of the element if it exists so it can be interacted with later
// https://regexr.com/7udrd
const hrefRE = /<a href=\"[^"]*\" class=\"([^"]*)\">|href=\"([^"]*)\"/g;
const htmlIncludesLinks = content.includes('http') && hrefRE.test(token.content);

if (htmlIncludesLinks) {
const matches = content.match(hrefRE);

matches.forEach(anchorElement => {
if (!anchorElement.includes('.gov')) {
if (!anchorElement.includes('class=')) {
if (!anchorElement.includes('usa-link--external')) {
// Since no class is present, we can safely just append our classes after the href property
const newUrl = anchorElement + ' class="usa-link usa-link--external"';
content = content.replace(anchorElement, newUrl);
tokens[idx].content = content;
}
} else {
// Handle URLs with classes already present
const classRE = /class="([^"]*)"/;
const [classString, oldClassList] = anchorElement.match(classRE);
const newClassList = oldClassList + ' usa-link usa-link--external';

// If someone uses the class property but doesn't actually put any classes in it, the class list will be empty
if (classString === 'class=""') {
content = content.replace(classString, 'class="usa-link usa-link--external"');
} else {
content = content.replace(oldClassList, newClassList);
}
tokens[idx].content = content;
}
}
});
}

return defaultHtmlBlockRender(tokens, idx, options, env, self);
}

// Also need to add icon links to any html style links
const inlineHTMLDefaultRender = markdownLibrary.renderer.rules.html_inline ||
function(tokens, idx, options, env, self) {
Expand Down
2 changes: 1 addition & 1 deletion _includes/footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<div class="grid-col usa-dark-background">
<p class="margin-bottom-0 text-white"> This project is maintained by <a class="usa-link text-primary-dark" href="https://18f.gsa.gov/">18F</a></p>
<p class="margin-bottom-0 margin-top-05 text-white"> Learn more about our <a class="usa-link text-primary-dark" href="https://18f.gsa.gov/linking-policy/">Linking Policy</a></p>
<p class="margin-top-05 text-white"> Have questions or would like to contact us? Email us at <a class="usa-link text-primary-dark" href="mailto:18@gsa.gov">18F@gsa.gov</a></p>
<p class="margin-top-05 text-white"> Have questions or would like to contact us? Email us at <a class="usa-link text-primary-dark" href="mailto:18f@gsa.gov">18F@gsa.gov</a></p>
</div>
</div>
</div>
Expand Down

0 comments on commit fb9afc8

Please sign in to comment.