Open Bilalzaiter opened 1 year ago
To modify the content of a given HTML page and save it with a new name using an array of page names and content, you can use a combination of HTML, CSS, and JavaScript. Here’s one way to approach it:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>{{title}}</title>
<link rel="stylesheet" href="../css/style.css" />
</head>
<body>
<header>
<h1>{{heading}}</h1>
</header>
<main>
<p>{{content}}</p>
</main>
<footer>{{footer}}</footer>
</body>
</html>
const pages = [
{
name: 'page1.html',
title: 'Page 1',
heading: 'Welcome to Page 1',
content: 'This is the content for Page 1.',
footer: 'Copyright © 2021'
},
{
name: 'page2.html',
title: 'Page 2',
heading: 'Welcome to Page 2',
content: 'This is the content for Page 2.',
footer: 'All rights reserved'
}
];
pages.forEach(page => {
// Replace placeholders with actual content
const html = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>${page.title}</title>
<link rel="stylesheet" href="../css/style.css" />
</head>
<body>
<header>
<h1>${page.heading}</h1>
</header>
<main>
<p>${page.content}</p>
</main>
<footer>${page.footer}</footer>
</body>
</html>
`;
// Save the modified HTML to a new file
const blob = new Blob([html], {type: 'text/html'});
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = page.name;
link.click();
});
link.download
property to set a default filename for each file.Note that this is just one approach and there are many ways to accomplish this task depending on your specific requirements and constraints.
give another idea
Another idea to modify the content of a given HTML page and save it with a new name using an array of page names and content is by leveraging a build tool like Gulp or Grunt. Here's a brief overview of the steps involved:
src/html/
dist/html/
const pages = [
{
name: 'page1.html',
title: 'Page 1',
heading: 'Welcome to Page 1',
content: 'This is the content for Page 1.',
footer: 'Copyright © 2021'
},
{
name: 'page2.html',
title: 'Page 2',
heading: 'Welcome to Page 2',
content: 'This is the content for Page 2.',
footer: 'All rights reserved'
}
];
gulp-html-replace
, gulp-htmlmin
, gulp-rename
pages
array, replace their placeholders, minify the HTML and output them to the destination directory. Here's an example using Gulp:const gulp = require('gulp');
const htmlReplace = require('gulp-html-replace');
const htmlmin = require('gulp-htmlmin');
const rename = require('gulp-rename');
const pages = [
{
name: 'page1.html',
title: 'Page 1',
heading: 'Welcome to Page 1',
content: 'This is the content for Page 1.',
footer: 'Copyright © 2021'
},
{
name: 'page2.html',
title: 'Page 2',
heading: 'Welcome to Page 2',
content: 'This is the content for Page 2.',
footer: 'All rights reserved'
}
];
function modifyAndSaveHtml() {
return gulp.src('src/html/*.html')
.pipe(htmlReplace({
title: '{{title}}',
heading: '{{heading}}',
content: '{{content}}',
footer: '{{footer}}'
}, {
keepUnassigned: true
}))
.pipe(htmlmin({
collapseWhitespace: true
}))
.pipe(rename(function (path) {
const page = pages.find(p => p.name === path.basename + path.extname);
path.basename = page.title.toLowerCase();
}))
.pipe(gulp.dest('dist/html/'));
}
exports.default = modifyAndSaveHtml;
gulp
command. The script will loop through each page in the pages
array, replace their placeholders, minify the HTML and output them to the destination directory dist/html/
.This is just one way to modify and save HTML pages with new content and names using an array of page names and content. Note that, depending on the complexity of your project or build process, you may need to modify or adapt this approach to suit your specific needs.
third way
Another way to modify a given HTML page's content and save it with a new name using an array of page names and content is to use a Node.js script. This approach allows you to use the popular libraries like Cheerio for parsing HTML content and File system for reading and writing files.
Here's a brief overview of the steps involved
src/html/
dist/html/
const pages = [
{
name: 'page1.html',
title: 'Page 1',
heading: 'Welcome to Page 1',
content: 'This is the content for Page 1.',
footer: 'Copyright © 2021'
},
{
name: 'page2.html',
title: 'Page 2',
heading: 'Welcome to Page 2',
content: 'This is the content for Page 2.',
footer: 'All rights reserved'
}
];
fs
library to read the HTML pages in the source directory, modify their content, and save them to the destination directory with the new names using the Cheerio
library. Here's an example:const fs = require('fs');
const cheerio = require('cheerio');
const pages = [
{
name: 'page1.html',
title: 'Page 1',
heading: 'Welcome to Page 1',
content: 'This is the content for Page 1.',
footer: 'Copyright © 2021'
},
{
name: 'page2.html',
title: 'Page 2',
heading: 'Welcome to Page 2',
content: 'This is the content for Page 2.',
footer: 'All rights reserved'
}
];
pages.forEach((page) => {
const filename = page.name.split('.html')[0];
const sourceHtml = fs.readFileSync(`src/html/${page.name}`, 'utf8');
const $ = cheerio.load(sourceHtml);
// Replace placeholders with actual content
$('title').text(page.title);
$('h1').text(page.heading);
$('p').text(page.content);
$('footer').text(page.footer);
// Save the modified HTML to a new file
const destHtml = $.html();
const destPath = `dist/html/${filename}-${Date.now()}.html`;
fs.writeFile(destPath, destHtml, (err) => {
if (err) throw err;
console.log(`✅ ${page.name} has been modified and saved as ${destPath}`);
});
});
node
command. The script will loop through each page in the pages
array, modify their content, and save them to the destination directory with the new names using fs
and Cheerio
.Note that, depending on the complexity of your project or the structure of your HTML pages, you may need to modify or adapt this approach to suit your specific needs.
can you help me to transform all the content of this html code to template literal and it's content to variable <!DOCTYPE html>
how to modify a given html page content and save it in a new name while giving an array of pages names and content array?