Skip to content

Instantly share code, notes, and snippets.

@mark05e
Created June 10, 2023 19:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mark05e/c8de42c387d0ee9639ebd715e9f3c10c to your computer and use it in GitHub Desktop.
Save mark05e/c8de42c387d0ee9639ebd715e9f3c10c to your computer and use it in GitHub Desktop.
Powershell script to insert js and css contents into an html
# Define the HTML file path
$htmlFilePath = "main-template.html"
# Define the CSS file path
$cssFilePath = "style.css"
# Define the JS file path
$jsFilePath = "functions.js"
# Define the output file path
$outputFilePath = "main.html"
# Read the CSS file content
$cssContent = Get-Content -Path $cssFilePath -Raw
# Read the JS file content
$jsContent = Get-Content -Path $jsFilePath -Raw
# Read the HTML file content
$htmlContent = Get-Content -Path $htmlFilePath -Raw
# Define the CSS start and end tags
$cssStartTag = "<!--CSS_START-->"
$cssEndTag = "<!--CSS_END-->"
# Define the JS start and end tags
$jsStartTag = "<!--JS_START-->"
$jsEndTag = "<!--JS_END-->"
# Replace the CSS content in the HTML file
$cssReplacement = "$cssStartTag`n<style>`n$cssContent`n</style>`n$cssEndTag"
$htmlContent = $htmlContent -replace "(?s)$cssStartTag.*?$cssEndTag", $cssReplacement
# Replace the JS content in the HTML file
$jsReplacement = "$jsStartTag`n<script>`n$jsContent`n</script>`n$jsEndTag"
$htmlContent = $htmlContent -replace "(?s)$jsStartTag.*?$jsEndTag", $jsReplacement
# Save the modified HTML content to the output file
$htmlContent | Set-Content -Path $outputFilePath
// js functions
<!DOCTYPE html>
<html>
<head>
<title>Simple HTML Page</title>
<!--CSS_START-->
<style><!--CSS_CONTENT--></style>
<!--CSS_END-->
</head>
<body>
<h1>Hello, World!</h1>
<!--JS_START-->
<script><!--JS_CONTENT--></script>
<!--JS_END-->
</body>
</html>
// css style
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment