Skip to content

Commit 5a88cb6

Browse files
committed
Add highlighting and blur magic comments
1 parent bcef9d7 commit 5a88cb6

File tree

5 files changed

+89
-5
lines changed

5 files changed

+89
-5
lines changed

docusaurus.config.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const connectDropdown = fs.readFileSync(
1717
'./src/components/NavDropdown/ConnectMetaMask.html',
1818
'utf-8'
1919
)
20+
const baseUrl = process.env.DEST || '/';
2021
const embedDropdown = fs.readFileSync('./src/components/NavDropdown/EmbedMetaMask.html', 'utf-8')
2122
const extendDropdown = fs.readFileSync('./src/components/NavDropdown/ExtendScale.html', 'utf-8')
2223
const npm2yarnPlugin = [require('@docusaurus/remark-plugin-npm2yarn'), { sync: true }]
@@ -28,7 +29,7 @@ const config = {
2829
title: 'MetaMask developer documentation',
2930
// tagline: '',
3031
url: 'https://docs.metamask.io',
31-
baseUrl: process.env.DEST || '/', // overwritten in github action for staging / latest
32+
baseUrl, // overwritten in github action for staging / latest
3233
onBrokenLinks: 'warn',
3334
onBrokenMarkdownLinks: 'warn',
3435
favicon: 'img/favicons/favicon-96x96.png',
@@ -110,6 +111,16 @@ const config = {
110111
trailingSlash: true,
111112

112113
scripts: [
114+
{
115+
src: baseUrl + "js/fix-trailing-slash.js",
116+
async: false,
117+
defer: false,
118+
},
119+
{
120+
src: baseUrl + "js/code-focus.js",
121+
async: false,
122+
defer: true,
123+
},
113124
{
114125
src: 'https://cmp.osano.com/AzZMxHTbQDOQD8c1J/84e64bce-4a70-4dcc-85cb-7958f22b2371/osano.js',
115126
},
@@ -544,6 +555,21 @@ const config = {
544555
theme: codeTheme,
545556
additionalLanguages: ['csharp', 'gradle', 'bash', 'json'],
546557
magicComments: [
558+
{
559+
className: 'theme-code-block-highlighted-line',
560+
line: 'highlight-next-line',
561+
block: { start: 'highlight-start', end: 'highlight-end' },
562+
},
563+
{
564+
className: "code-unfocus",
565+
line: "unfocus-next-line",
566+
block: { start: "unfocus-start", end: "unfocus-end" },
567+
},
568+
{
569+
className: "code-focus",
570+
line: "focus-next-line",
571+
block: { start: "focus-start", end: "focus-end" },
572+
},
547573
{
548574
className: 'git-diff-remove',
549575
line: 'remove-next-line',
@@ -574,7 +600,7 @@ const config = {
574600
// Optional: Replace parts of the item URLs from Algolia. Useful when using the same search index for multiple deployments using a different baseUrl. You can use regexp or string in the `from` param. For example: localhost:3000 vs myCompany.com/docs
575601
replaceSearchResultPathname: {
576602
from: '/',
577-
to: process.env.DEST || '/',
603+
to: baseUrl,
578604
},
579605

580606
// Optional: Algolia search parameters

src/pages/quick-start/builder/highlight.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function highlightStart(fileContent: string, variableName: string): string {
99
const contentByLine = fileContent.split(`\n`)
1010
for (let i = 0; i < contentByLine.length; i += 1) {
1111
if (contentByLine[i].includes(`IMP START - ${variableName}`)) {
12-
contentByLine[i] = '// highlight-start'
12+
contentByLine[i] = `//highlight-start \n// focus-start`
1313
}
1414
}
1515
return contentByLine.join('\n')
@@ -25,7 +25,7 @@ function highlightEnd(fileContent: string, variableName: string): string {
2525
const contentByLine = fileContent.split(`\n`)
2626
for (let i = 0; i < contentByLine.length; i += 1) {
2727
if (contentByLine[i].includes(`IMP END - ${variableName}`)) {
28-
contentByLine[i] = '// highlight-end'
28+
contentByLine[i] = `//highlight-end \n// focus-end`
2929
}
3030
}
3131
return contentByLine.join('\n')

src/scss/custom.scss

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,43 @@ ol {
652652
}
653653
}
654654

655+
/* Code Focus & Blur Effects */
656+
%blur-transition {
657+
filter: blur(0.5px);
658+
opacity: 0.3;
659+
transition:
660+
filter 0.2s ease,
661+
opacity 0.2s ease;
662+
will-change: filter, opacity;
663+
}
664+
665+
%clear-blur {
666+
filter: none;
667+
opacity: 1;
668+
}
669+
670+
.code-unfocus {
671+
@extend %blur-transition;
672+
}
673+
674+
.blurred-code {
675+
.token-line:not(.code-focus) {
676+
@extend %blur-transition;
677+
}
678+
679+
.code-focus {
680+
@extend %clear-blur;
681+
}
682+
683+
&:hover .token-line:not(.code-focus) {
684+
@extend %clear-blur;
685+
}
686+
}
687+
688+
code:hover .code-unfocus {
689+
@extend %clear-blur;
690+
}
691+
655692
.git-diff-remove {
656693
background-color: #ff000020;
657694
display: block;
@@ -846,4 +883,4 @@ div[class^='theme-announcement-bar'] {
846883
/* Add scroll padding to account for sticky product banner */
847884
html {
848885
scroll-padding-top: 10rem;
849-
}
886+
}

static/js/code-focus.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
document.addEventListener("DOMContentLoaded", function () {
2+
const observer = new MutationObserver(function (mutations) {
3+
mutations.forEach(function (mutation) {
4+
const codeBlocks = document.querySelectorAll("code");
5+
6+
codeBlocks.forEach(function (codeBlock) {
7+
const hasCodeFocus = codeBlock.querySelector(".code-focus") !== null;
8+
9+
if (hasCodeFocus) {
10+
codeBlock.classList.add("blurred-code");
11+
}
12+
});
13+
});
14+
});
15+
16+
// Start observing the document for additions to the DOM
17+
observer.observe(document.body, { childList: true, subtree: true });
18+
});

static/js/fix-trailing-slash.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
if (window && window.location && window.location.pathname.endsWith('/') && window.location.pathname !== '/') {
2+
window.history.replaceState('', '', window.location.pathname.substr(0, window.location.pathname.length - 1))
3+
}

0 commit comments

Comments
 (0)