-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
62 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System.Text.RegularExpressions; | ||
|
||
namespace EasyHtmlToolkit.Extensions | ||
{ | ||
public class FileMinimizer | ||
{ | ||
public string GetMinimizedFileContent(string filePath) | ||
{ | ||
try | ||
{ | ||
// Read the file content | ||
string fileContent = File.ReadAllText(filePath); | ||
|
||
// Minimize the file content (remove comments, unnecessary whitespace, etc.) | ||
string minimizedContent = MinimizeContent(fileContent); | ||
|
||
return minimizedContent; | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine($"Error reading or minimizing file: {ex.Message}"); | ||
return null; | ||
Check warning on line 22 in EasyHtmlToolkit/Extensions/FileMinimizer.cs GitHub Actions / build
|
||
} | ||
} | ||
|
||
private string MinimizeContent(string content) | ||
{ | ||
// Remove comments (HTML, CSS, JS) and unnecessary whitespace | ||
content = RemoveComments(content); | ||
content = RemoveWhitespace(content); | ||
|
||
return content; | ||
} | ||
|
||
private string RemoveComments(string content) | ||
{ | ||
// Remove HTML comments: <!-- ... --> | ||
content = Regex.Replace(content, @"<!--(.*?)-->", "", RegexOptions.Singleline); | ||
|
||
// Remove CSS comments: /* ... */ | ||
content = Regex.Replace(content, @"/\*(.*?)\*/", "", RegexOptions.Singleline); | ||
|
||
// Remove JavaScript comments: // ... or /* ... */ | ||
content = Regex.Replace(content, @"\/\/(.+?)\n|\/\*(.*?)\*\/", "", RegexOptions.Singleline); | ||
|
||
return content; | ||
} | ||
|
||
private string RemoveWhitespace(string content) | ||
{ | ||
// Remove unnecessary whitespace using regex | ||
content = Regex.Replace(content, @"\s+", " "); | ||
|
||
return content; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters