Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion assets/scripts/components/copy-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function initCopyCode () {
addCopyButton(['shell', 'json', 'yaml', 'sql', 'bash', 'hcl', 'python'])

// Add Event Listener
const copyButtons = document.querySelectorAll(['.js-copy-button', '#tryRuleModal .copy-icon']);
const copyButtons = document.querySelectorAll(['.js-copy-button', '.js-copy-inline-button', '#tryRuleModal .copy-icon']);

if (copyButtons.length) {
copyButtons.forEach(btn => {
Expand Down Expand Up @@ -60,6 +60,7 @@ function copyCode (btn){

function getCode (btn){
return btn.closest('.code-snippet')?.querySelector('code') // for markdown fenced code blocks
|| btn.closest('.copyable-inline-code')?.querySelector('code') // for inline copyable code
|| btn.previousElementSibling.querySelector('td:last-child code'); // for try-rule modal code examples on the static analysis rule pages
}

Expand All @@ -70,6 +71,18 @@ function updateCopyBtnText(btn){
setTimeout(function() {
btn.textContent = "Copy"
}, 1000)
}else if(btn?.classList.contains('js-copy-inline-button')){
// if inline copy button clicked, swap icons
const copyIcon = btn.querySelector('.copy-icon');
const checkIcon = btn.querySelector('.check-icon');
if (copyIcon && checkIcon) {
copyIcon.style.display = 'none';
checkIcon.style.display = 'inline';
setTimeout(function() {
copyIcon.style.display = 'inline';
checkIcon.style.display = 'none';
}, 1000)
}
}else{
// if copy icon clicked in the try-rule modal, change the tooltip
const copyTooltip = Tooltip.getInstance(btn);
Expand Down
50 changes: 50 additions & 0 deletions assets/styles/pages/_global.scss
Original file line number Diff line number Diff line change
Expand Up @@ -1013,3 +1013,53 @@ dl {
padding-top: 24px !important;
}
}

// Inline copyable code with copy button
.copyable-inline-code {
display: inline-flex;
align-items: center;
gap: 3px;
position: relative;
white-space: nowrap;

code {
padding: 1px 3px;
border-radius: 3px;
font-family: RobotoMono-Regular, SFMono-Regular, Menlo, Monaco, Consolas, Liberation Mono, Courier New, monospace;
}

.js-copy-inline-button {
opacity: 0;
padding: 2px;
line-height: 0;
border: none;
border-radius: 3px;
transition: opacity 0.2s ease;
background-color: transparent;
cursor: pointer;
display: inline-flex;
align-items: center;
justify-content: center;

svg {
fill: #632ca6;
transition: fill 0.2s ease;
}

&:hover {
background-color: rgba(99, 44, 166, 0.1);

svg {
fill: #4a1f7d;
}
}
}

&:hover .js-copy-inline-button {
opacity: 1;
}

.check-icon {
fill: #00a854 !important;
}
}
11 changes: 11 additions & 0 deletions layouts/shortcodes/copyable-code.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<span class="copyable-inline-code">
<code>{{ .Inner }}</code>
<button class="js-copy-inline-button" aria-label="Copy code">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 192 192" width="14" height="14" class="copy-icon">
<path d="M176,140H160V32H52V16H176ZM140,52H16V176H140ZM32,68h92v92H32Z"/>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 192 192" width="14" height="14" class="check-icon" style="display:none">
<path d="M65,165.313,5.343,105.657,16.657,94.343,65,142.687,175.343,32.343l11.314,11.314Z"/>
</svg>
</button>
</span>
Loading