-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #168 from GSA/mmeyer/refactor-link-plugin
refactor link / image plugins
- Loading branch information
Showing
4 changed files
with
123 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* The purpose of this is to process links in content sources to prevent | ||
* local links on cloud.gov pages feature branches from breaking. | ||
* Markdown links like [some text](somelink) will be prefixed with the | ||
* Base_URL and have the class `usa-link` added. If the link is a | ||
* non-smartpay link it will additionally get the usa-link--external class | ||
* | ||
* Inline html links like <a href="somelink">some text</> | ||
* will only be prefixed with the baseURL. Classes can be added inline | ||
*/ | ||
|
||
|
||
import {visit} from 'unist-util-visit' | ||
|
||
import path from 'path' | ||
|
||
function isInternalDomain(url) { | ||
try { | ||
const domain = new URL(url) | ||
return domain.hostname.endsWith('smartpay.gsa.gov') | ||
} catch(e) { | ||
// this represents urls like "/some/path" without domain | ||
return true | ||
} | ||
} | ||
|
||
function process_mdx_anchor(node, baseURL) { | ||
/** | ||
* Process raw links in source content | ||
* like <a href="/some_link">text</a> | ||
*/ | ||
|
||
node.attributes.forEach(attr => { | ||
if (attr.name === 'href' && attr.value.startsWith('/') && !attr.value.startsWith('//')) { | ||
attr.value = path.join('/', baseURL, attr.value) | ||
} | ||
}); | ||
} | ||
|
||
function process_markdown_link(node, baseURL) { | ||
/** | ||
* Process raw links in source content | ||
* like [some text](/some_link) | ||
*/ | ||
|
||
const properties = node.properties | ||
properties.className = isInternalDomain(properties.href) | ||
? 'usa-link' | ||
: 'usa-link usa-link--external'; | ||
|
||
if (properties.href.startsWith('/') && !properties.href.startsWith('//')) { | ||
properties.href = path.join('/', baseURL, properties.href) | ||
} | ||
} | ||
export default (options) => { | ||
return tree => { | ||
visit(tree, [{"type": "element", "tagName": "a"}, {"type":'mdxJsxTextElement', "name": "a"}], (node, index, parent) => { | ||
if (node.type === 'mdxJsxTextElement') { | ||
process_mdx_anchor(node, options.baseURL) | ||
} else { | ||
process_markdown_link(node, options.baseURL) | ||
} | ||
}) | ||
} | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* The purpose of this is to process image in content sources to prevent | ||
* local srcs (those starting with `/`) on cloud.gov pages feature branches from breaking. | ||
* This should work for both markdown image like ![some text](somelink) as well as | ||
* inline html image like <img src="somelink" alt="some text"/> | ||
*/ | ||
|
||
|
||
import {visit} from 'unist-util-visit' | ||
|
||
import path from 'path' | ||
|
||
|
||
function process_mdx_image(node, baseURL) { | ||
/** | ||
* Process raw img tags in source content | ||
* like <img src="/some_link" /> | ||
*/ | ||
|
||
node.attributes.forEach(attr => { | ||
if (attr.name === 'src' && attr.value.startsWith('/') && !attr.value.startsWith('//')) { | ||
attr.value = path.join('/', baseURL, attr.value) | ||
} | ||
}); | ||
} | ||
|
||
function process_markdown_image(node, baseURL) { | ||
/** | ||
* Process raw links in source content | ||
* like ![some text](/some_link) | ||
*/ | ||
|
||
const properties = node.properties | ||
|
||
if (properties.src.startsWith('/') && !properties.src.startsWith('//')) { | ||
properties.src = path.join('/', baseURL, properties.src) | ||
} | ||
} | ||
export default (options) => { | ||
return tree => { | ||
visit(tree, [{"type": "element", "tagName": "img"}, {"type":'mdxJsxFlowElement', "name": "img"}], (node, index, parent) => { | ||
if (node.type === 'mdxJsxFlowElement') { | ||
process_mdx_image(node, options.baseURL) | ||
} else { | ||
process_markdown_image(node, options.baseURL) | ||
} | ||
}) | ||
} | ||
}; | ||
|
||
|
This file was deleted.
Oops, something went wrong.