Skip to content

Commit 57e176e

Browse files
Merge pull request #3507 from department-of-veterans-affairs/authed-patterns/aw/185/add-syntax-highlighting-for-markdown-code-blocks
Add syntax highlighting for markdown code blocks
2 parents b956337 + 4dfac84 commit 57e176e

File tree

6 files changed

+251
-94
lines changed

6 files changed

+251
-94
lines changed

_config.yml

+6-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ example_library_name: "VADS Example Library"
99
resource_library_name: "VADS Templates, Patterns, and Forms"
1010

1111
# Common links
12-
figma_example_library: https://www.figma.com/file/JDFpGLIojfuQwANXScQjqe/VADS-Component-Examples?type=design&node-id=0%3A1&mode=design&t=c29j3xBxzMAyLWxu-1
12+
figma_example_library: https://www.figma.com/file/JDFpGLIojfuQwANXScQjqe/VADS-Component-Examples?type=design&node-id=0%3A1&mode=design&t=c29j3xBxzMAyLWxu-1
1313
figma_component_library: https://www.figma.com/file/afurtw4iqQe6y4gXfNfkkk/VADS-Component-Library?type=design&node-id=121%3A1484&mode=design&t=ygQCLqlAL5VdSPil-1
1414
figma_templates_library: https://www.figma.com/file/4A3O3mVx4xDAKfHE7fPF1U/VADS-Templates%2C-Patterns%2C-and-Forms?type=design&node-id=2988%3A29744&mode=design&t=A89mK0w9KGp2uWIR-1
1515
figma_annotations_library: https://www.figma.com/file/CZcnWfQOwtLqPm4WA5paYG/VADS-Web-Annotation-Kit?type=design&node-id=415%3A1135&mode=design&t=V0V5YZrcbM7VnWa6-1
@@ -34,16 +34,16 @@ new_pattern_template_raw_link: https://raw.githubusercontent.com/department-of-v
3434
update_documentation_link: https://github.com/department-of-veterans-affairs/vets-design-system-documentation/issues/new?assignees=&labels=vsp-design-system-team&template=2_documentation_request_form.yml
3535
uswds_link: https://designsystem.digital.gov/
3636

37-
3837
# People
39-
sketch_for_teams_admin: "Kevin Hoffman"
38+
sketch_for_teams_admin: "Kevin Hoffman"
4039
sketch_library_owner: "Lillian Boot, Barb Denney"
4140
experimental_reviewer_1: "Matthew Dingee"
4241

43-
4442
# Build settings
4543
markdown: kramdown
46-
highlighter: none
44+
highlighter: rouge
45+
kramdown:
46+
syntax_highlighter: rouge
4747
plugins:
4848
- jekyll-last-modified-at
4949
- jekyll-redirect-from
@@ -91,7 +91,7 @@ exclude:
9191
- _foundation/layout/html
9292
- _foundation/utilities/html
9393

94-
# Default for building locally.
94+
# Default for building locally.
9595
# See configuration files in jekyll-configs/ for environment overrides.
9696
storybook_path: "http://127.0.0.1:6006"
9797
storybook_prod_uswds_path: "https://design.va.gov/storybook/?path=/docs/uswds"

src/_about/developers/using-web-components.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ If the Web Component has a custom event that you need to use and you're **not**
148148
const element = document.querySelector('va-example-component');
149149
const exampleCallback = event => { console.log(event.detail) }
150150
element.addEventListener('vaChange', exampleCallback)
151-
<script>
151+
</script>
152152

153153
<va-example-component>
154154
```

src/_about/naming.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ It consists of three required parts.
2222

2323
When put together, the structure is `[global namespace]-[class prefix]-[BEM syntax]`, or:
2424

25-
```
26-
vads-c-component-name
25+
```css
26+
.vads-c-component-name
2727
```
2828

2929
There are other variants on the naming convention as well.
@@ -53,26 +53,26 @@ While the BEM syntax typically results in longer class names, it is excellent fo
5353

5454
For example, an alert can be considered a **block** (for brevity, we will not use the full naming convention here).
5555

56-
```
56+
```css
5757
.alert {}
5858
```
5959

6060
The alert might have several child **elements** unique to that block, perhaps a header or a body. Element names are the block + the element name, separated by two underscores (`__`).
6161

62-
```
62+
```css
6363
.alert__header {}
6464
.alert__body {}
6565
```
6666

6767
There can be different types of alerts, such as success or error. These are _types_ are known as **modifiers**. Modifier names are the block + modifier name, separated by two hyphens (`--`). Modifier selectors are secondary class names that contain only the modified properties.
6868

69-
```
69+
```css
7070
.alert--success {}
7171
.alert--error {}
7272
```
7373

7474
When put together, we can have something like:
75-
```
75+
```html
7676
<div class="alert alert--success">
7777
<h2 class="alert__header">This is the alert heading</h2>
7878
<div class="alert__body">

src/assets/javascripts/components/code-snippets.js

+46-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,56 @@ if (showSnippetButton.length) {
55
}
66

77
for (var i = 0; i < showSnippetButton.length; i++) {
8-
showSnippetButton[i].addEventListener("click", function(e) {
8+
showSnippetButton[i].addEventListener("click", function (e) {
99
if (this.innerHTML == original_text) {
1010
this.innerHTML = '<span class="fas fa-minus vads-u-margin-right--0p5"></span> Hide HTML';
1111
}
1212
else {
1313
this.innerHTML = original_text;
1414
}
1515
});
16-
}
16+
}
17+
18+
// allow markdown code blocks ``` to be copied to clipboard via va-button
19+
document.addEventListener('DOMContentLoaded', function () {
20+
const codeBlocks = document.querySelectorAll('pre.highlight');
21+
22+
codeBlocks.forEach(function (codeBlock) {
23+
// Create container for proper button positioning
24+
const container = document.createElement('div');
25+
container.className = 'highlight-container';
26+
27+
// Create wrapper div copy button
28+
const copyButtonWrapper = document.createElement('div');
29+
30+
// Create VA button
31+
const copyButton = document.createElement('va-button');
32+
copyButton.setAttribute('secondary', '');
33+
copyButton.setAttribute('text', 'Copy');
34+
copyButton.className = 'copy-button';
35+
36+
copyButtonWrapper.appendChild(copyButton);
37+
38+
// Wrap code block
39+
codeBlock.parentNode.insertBefore(container, codeBlock);
40+
container.appendChild(codeBlock);
41+
container.appendChild(copyButtonWrapper);
42+
43+
// Add click handler
44+
copyButton.addEventListener('click', async function () {
45+
try {
46+
const code = codeBlock.textContent;
47+
await navigator.clipboard.writeText(code);
48+
49+
// Show success state
50+
copyButton.setAttribute('text', 'Copied!');
51+
setTimeout(() => {
52+
copyButton.setAttribute('text', 'Copy');
53+
}, 2000);
54+
} catch (err) {
55+
console.error('Failed to copy:', err);
56+
copyButton.setAttribute('text', 'Error');
57+
}
58+
});
59+
});
60+
});

src/assets/stylesheets/_components/_code-snippets.scss

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
border-top-color: $color-gray-lightest !important;
3131
}
3232

33+
.site-code-snippet__content .highlight-container {
34+
border: 0px !important;
35+
}
36+
3337
.site-code-snippet__content[aria-hidden="true"] {
3438
display: block !important;
3539
max-height: 96px;

0 commit comments

Comments
 (0)