i have some multiple contents(got from gpt) in markdown format with all mathjax and other support,
now i want a function in node js that creates a html file(with tailwind cdn and more to render) which has these features
a webpage with 3-4 tabs each rendering the content passed in array
the content passed will be as follows:
[
{
"tabName : "Chat GPT",
"content" : "markdonw content with all the things"
}
]
Assistant
Certainly! Below is a Node.js function that generates an HTML file with Tailwind CSS and MathJax support. The HTML file includes tabs, each rendering the markdown content from the array you provide.
First, make sure you have Node.js installed on your machine. We'll use the following Node.js packages:
fs: To write the HTML file to the filesystem.
markdown-it: To convert markdown content to HTML.
You'll need to install the markdown-it package using npm:
bash
npm install markdown-it
Here's the complete code:
js
const fs = require('fs');
const markdownIt = require('markdown-it');
function generateHTMLFile(contentArray, outputFileName) {
const md = new markdownIt({
html: true,
linkify: true,
typographer: true,
});
let tabsNav = '';
let tabsContent = '';
// Loop through the content array to build tabs and content
contentArray.forEach((contentObj, index) => {
const tabId = 'tab' + index;
const tabName = contentObj.tabName || 'Tab ' + (index + 1);
const markdownContent = contentObj.content || '';
const htmlContentConverted = md.render(markdownContent);
// Build the tabs navigation buttons
tabsNav += `
<button class="tablinks" onclick="openTab(event, '${tabId}')">${tabName}</button>
`;
// Build the content for each tab
tabsContent += `
<div id="${tabId}" class="tabcontent">
${htmlContentConverted}
</div>
`;
});
// Assemble the final HTML content
const htmlContent = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Tabbed Content</title>
<!-- Tailwind CSS CDN -->
<script src="https://cdn.tailwindcss.com"></script>
<!-- MathJax CDN -->
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
<script id="MathJax-script" async
src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js">
</script>
<style>
/* Style the tab links */
.tablinks {
@apply bg-gray-200 float-left border-none outline-none cursor-pointer py-2 px-4 transition duration-300 text-lg;
}
.tablinks:hover {
@apply bg-gray-300;
}
.tabcontent {
display: none;
padding: 1rem;
border-top: none;
}
.tabcontent {
animation: fadeEffect 1s;
}
@keyframes fadeEffect {
from {opacity: 0;}
to {opacity: 1;}
}
</style>
</head>
<body class="font-sans antialiased">
<div class="tab flex">
${tabsNav}
</div>
<div class="tab-contents">
${tabsContent}
</div>
<script>
function openTab(evt, tabId) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].classList.remove("bg-blue-500", "text-white");
}
document.getElementById(tabId).style.display = "block";
evt.currentTarget.classList.add("bg-blue-500", "text-white");
}
// Open the first tab by default
document.addEventListener("DOMContentLoaded", function(){
document.querySelector('.tablinks').click();
});
</script>
</body>
</html>
`;
// Write the HTML content to the specified output file
fs.writeFileSync(outputFileName, htmlContent, 'utf8');
console.log(`HTML file "${outputFileName}" has been generated.`);
}
// Example usage:
const contents = [
{
"tabName": "Chat GPT",
"content": `
# Welcome to Chat GPT
This is a sample content with **MathJax** support.
Here is an equation:
\\[
E = mc^2
\\]
And here's some inline math: \\( a^2 + b^2 = c^2 \\ ).
`
},
{
"tabName": "Second Tab",
"content": `
## Second Tab Content
More content here with a list:
- Item 1
- Item 2
- Item 3
Code snippet:
\`\`\`javascript
console.log('Hello, world!');
\`\`\`
`
}
];
generateHTMLFile(contents, 'output.html');
Explanation:
Markdown Conversion: The function uses markdown-it to convert the markdown content to HTML, including support for code blocks, lists, headers, and more.
MathJax Support: By including the MathJax CDN script, any LaTeX math expressions written in your markdown content (using $$...$$ for display math or $...$ for inline math) will be rendered correctly.
Tailwind CSS: The Tailwind CSS CDN is included to style the page. We're using the @apply directive to apply Tailwind utility classes within the <style> tags.
Tabs Functionality: Simple JavaScript functions handle tab switching. When a tab is clicked, the corresponding content is displayed.
Default Tab: On page load, the first tab is automatically opened.
How to Use:
Install Dependencies:
Make sure you have markdown-it installed. If not, run:
bash
npm install markdown-it
Prepare Your Content Array:
Create an array of objects where each object has a tabName and content (as markdown string). Replace the sample contents array with your own content.
Run the Function:
Call generateHTMLFile(contents, 'output.html'); with your content array and desired output file name.
View the Output:
After running the script, open output.html in your web browser to see the rendered page with tabs.
Ensure that your markdown content includes proper MathJax delimiters for mathematical expressions:
Inline Math: Use \\( ... \\) or $ ... $
Display Math: Use \\[ ... \\] or $$ ... $$
The function includes basic styling for tabs. You can customize the styles further using Tailwind CSS classes or your own CSS.
If you prefer to process more complex markdown or need additional plugins (e.g., for superscripts, footnotes), you can extend markdown-it with plugins.
Conclusion
This function should fulfill your requirement of generating an HTML file with multiple tabs, each rendering markdown content with MathJax and styled using Tailwind CSS. Feel free to customize and extend the function as needed for your specific use case.