Skip to content

Commit 365db92

Browse files
committed
Add highlighting and blur magic comments
1 parent 40f9596 commit 365db92

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 }]
@@ -25,7 +26,7 @@ const config = {
2526
title: 'MetaMask developer documentation',
2627
// tagline: '',
2728
url: 'https://docs.metamask.io',
28-
baseUrl: process.env.DEST || '/', // overwritten in github action for staging / latest
29+
baseUrl, // overwritten in github action for staging / latest
2930
onBrokenLinks: 'warn',
3031
onBrokenMarkdownLinks: 'warn',
3132
favicon: 'img/metamask-fox.svg',
@@ -55,6 +56,16 @@ const config = {
5556
trailingSlash: true,
5657

5758
scripts: [
59+
{
60+
src: baseUrl + "js/fix-trailing-slash.js",
61+
async: false,
62+
defer: false,
63+
},
64+
{
65+
src: baseUrl + "js/code-focus.js",
66+
async: false,
67+
defer: true,
68+
},
5869
{
5970
src: 'https://cmp.osano.com/AzZMxHTbQDOQD8c1J/84e64bce-4a70-4dcc-85cb-7958f22b2371/osano.js',
6071
},
@@ -489,6 +500,21 @@ const config = {
489500
theme: codeTheme,
490501
additionalLanguages: ['csharp', 'gradle', 'bash', 'json'],
491502
magicComments: [
503+
{
504+
className: 'theme-code-block-highlighted-line',
505+
line: 'highlight-next-line',
506+
block: { start: 'highlight-start', end: 'highlight-end' },
507+
},
508+
{
509+
className: "code-unfocus",
510+
line: "unfocus-next-line",
511+
block: { start: "unfocus-start", end: "unfocus-end" },
512+
},
513+
{
514+
className: "code-focus",
515+
line: "focus-next-line",
516+
block: { start: "focus-start", end: "focus-end" },
517+
},
492518
{
493519
className: 'git-diff-remove',
494520
line: 'remove-next-line',
@@ -519,7 +545,7 @@ const config = {
519545
// 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
520546
replaceSearchResultPathname: {
521547
from: '/',
522-
to: process.env.DEST || '/',
548+
to: baseUrl,
523549
},
524550

525551
// 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)