Skip to content

Commit

Permalink
hacky way to convert plane MD to cards
Browse files Browse the repository at this point in the history
  • Loading branch information
nialexsan committed Aug 19, 2023
1 parent 8b3d4da commit efe89f7
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 4 deletions.
6 changes: 5 additions & 1 deletion docs/tools/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ description: Essential tools for the Flow blockchain ecosystem

# Tools

<div id="tools" className="tools-cards">

## Node Providers

### Quick Node
Expand Down Expand Up @@ -227,4 +229,6 @@ The [Flow Client Library (FCL)](./clients/fcl-js/index.md) JS is a package used

### HTTP API

[Flow OpenAPI](/http-api) specification
[Flow OpenAPI](/http-api) specification

</div>
9 changes: 6 additions & 3 deletions docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ const editUrl = ({ docPath }) => {
return `https://github.com/${owner}/${name}/tree/${branch}/${sourceDocPath}`;
};

const baseUrl = process.env.BASE_URL || '/docs/';

/** @type {import('@docusaurus/types').Config} */
const config = {
title: 'Flow Developer Portal',
Expand All @@ -149,7 +151,7 @@ const config = {
url: getUrl(),
// Set the /<baseUrl>/ pathname under which your site is served
// For GitHub pages deployment, it is often '/<projectName>/'
baseUrl: process.env.BASE_URL || '/docs/',
baseUrl,

// GitHub pages deployment config.
// If you aren't using GitHub pages, you don't need these.
Expand Down Expand Up @@ -596,7 +598,7 @@ const config = {
],
scripts: [
{
src: '/mixpanel.js',
src: `${baseUrl}mixpanel.js`,
async: true,
onload: mixpanelOnLoad,
},
Expand All @@ -609,7 +611,7 @@ const config = {
async: true,
},
{
src: '/hotjar.js',
src: `${baseUrl}hotjar.js`,
async: true,
},
{
Expand All @@ -621,6 +623,7 @@ const config = {
async: true,
},
],
clientModules: [require.resolve('./src//modules/toolscards.ts')],
};

module.exports = config;
23 changes: 23 additions & 0 deletions src/css/custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,26 @@
#navbar-discord {
content: var(--image-navbar-discord);
}

.tools-cards > div {
display: grid;
gap: 8px;
grid-template-columns: repeat(2, 1fr);
grid-template-areas: "header header" "description description";
margin-bottom: 2rem;
}

.tools-cards > div > h2 {
grid-area: header;
}

.tools-cards > div > p {
grid-area: description;
}

.tools-cards > div > div {
padding: 1rem;
background-color: var(--ifm-card-background-color);
border-radius: var(--ifm-card-border-radius);
border: 1px solid var(--ifm-color-emphasis-200);
}
76 changes: 76 additions & 0 deletions src/modules/toolscards.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* convert a plane structure with [h2 p h3 p p h3 p p] to [div(h2 div(h3 p p) div(h3 p p))]
* in order to render cards in place of simple markdown with styles defined in src/css/custom.css
* @param param0
* @returns
*/
export function onRouteDidUpdate({ location, previousLocation }): void {
// Don't execute if we are still on the same page; the lifecycle may be fired
// because the hash changes (e.g. when navigating between headings)
if (location.pathname !== previousLocation?.pathname) {
const page = document.getElementById('tools');
if (page == null) {
return;
}

const htmlElements = page.children;

// Initialize variables to store the current section and subsection
let currentSection = document.createElement('div');
let currentCard = document.createElement('div');

const appendCard = (): void => {
const anchor: HTMLAnchorElement | null = currentCard.querySelector(
'a[href]:not([href^="#"])',
);
if (anchor != null) {
currentCard.addEventListener('click', (event) => {
if ((event?.target as HTMLAnchorElement)?.tagName === 'A') {
// If it's an anchor tag, let the click event on the anchor proceed as usual
return;
}
anchor.click();
});
currentCard.style.cursor = 'pointer';
}
currentSection.appendChild(currentCard);
};

// Initialize an array to store the parsed divs
document.createElement('div');

// Loop through the HTML elements
for (const element of Array.from(htmlElements)) {
switch (element.tagName) {
case 'H2': {
if (currentCard.children.length > 0) {
appendCard();
}
if (currentSection.children.length > 0) {
page.appendChild(currentSection);
}

currentSection = document.createElement('div');
currentSection.appendChild(element);
currentCard = document.createElement('div');
break;
}
case 'H3': {
if (currentCard.children.length > 0) {
appendCard();
}
currentCard = document.createElement('div');
currentCard.appendChild(element);
break;
}
default: {
if (currentCard.children.length === 0) {
currentSection.appendChild(element);
} else {
currentCard.appendChild(element);
}
}
}
}
}
}

0 comments on commit efe89f7

Please sign in to comment.