Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding protection string options for Issue #9 #10

Merged
merged 6 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "htmlfy",
"version": "0.5.0",
"version": "0.5.1",
"description": "HTML formatter yo!. Prettify, minify, and more!",
"scripts": {
"check": "tsc",
Expand Down
3 changes: 2 additions & 1 deletion src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ export const CONFIG = {
ignore: [],
strict: false,
tab_size: 2,
trim: []
trim: [],
protection_string: '',
j4w8n marked this conversation as resolved.
Show resolved Hide resolved
}
6 changes: 4 additions & 2 deletions src/prettify.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,17 +159,19 @@ export const prettify = (html, config) => {
const ignore = validated_config.ignore.length > 0
trim = validated_config.trim

const protectionString = config?.protection_string || '_!i-£___£%_';
j4w8n marked this conversation as resolved.
Show resolved Hide resolved
j4w8n marked this conversation as resolved.
Show resolved Hide resolved

/* Protect ignored elements. */
if (ignore) {
html = ignoreElement(html, validated_config.ignore)
html = ignoreElement(html, validated_config.ignore, 'protect', protectionString)
}

html = preprocess(html)
html = process(html, validated_config.tab_size)

/* Unprotect ignored elements. */
if (ignore) {
html = ignoreElement(html, validated_config.ignore, 'unprotect')
html = ignoreElement(html, validated_config.ignore, 'unprotect', protectionString)
}

return html
Expand Down
1 change: 1 addition & 0 deletions src/types/internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export interface DefaultConfig {
strict: boolean;
tab_size: number;
trim: string[];
protection_string: string
}

type RecursiveRequired<T> = {
Expand Down
36 changes: 20 additions & 16 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,14 @@ export const mergeConfig = (dconfig, config) => {
*
* @param {string} html
* @param {string[]} ignore
* @param {string} [mode]
* @param {'protect'|'unprotect'} mode
* @param {string} protectionString
* @returns {string}
*/
export const ignoreElement = (html, ignore, mode = 'protect') => {
export const ignoreElement = (html, ignore, mode, protectionString) => {
for (let e = 0; e < ignore.length; e++) {
const regex = new RegExp(`<${ignore[e]}[^>]*>((.|\n)*?)<\/${ignore[e]}>`, "g")
html = html.replace(regex, mode === 'protect' ? protectElement : unprotectElement)
html = html.replace(regex, mode === 'protect' ? (match, capture) => protectElement(match, capture, protectionString) : (match, capture) => unprotectElement(match, capture, protectionString))
}

return html
Expand All @@ -81,16 +82,17 @@ export const ignoreElement = (html, ignore, mode = 'protect') => {
*
* @param {string} match
* @param {any} capture
* @param {string} protectionString
* @returns
*/
const protectElement = (match, capture) => {
const protectElement = (match, capture, protectionString) => {
return match.replace(capture, (match) => {
return match
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/\n/g, '&#10;')
.replace(/\r/g, '&#13;')
.replace(/\s/g, '&nbsp;')
.replace(/</g, '-' + protectionString + 'lt-')
.replace(/>/g, '-' + protectionString + 'gt-')
.replace(/\n/g, '-' + protectionString + 'nl-')
.replace(/\r/g, '-' + protectionString + 'cr-')
.replace(/\s/g, '-' + protectionString + 'ws-')
})
}

Expand Down Expand Up @@ -120,16 +122,17 @@ export const trimify = (html, trim) => {
*
* @param {string} match
* @param {any} capture
* @param {string} protectionString
* @returns
*/
const unprotectElement = (match, capture) => {
const unprotectElement = (match, capture, protectionString) => {
return match.replace(capture, (match) => {
return match
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>')
.replace(/&#10;/g, '\n')
.replace(/&#13;/g, '\r')
.replace(/&nbsp;/g, ' ')
.replace(new RegExp('-' + protectionString + 'lt-', "g"), '<')
.replace(new RegExp('-' + protectionString + 'gt-', "g"), '>')
.replace(new RegExp('-' + protectionString + 'nl-', "g"), '\n')
.replace(new RegExp('-' + protectionString + 'cr-', "g"), '\r')
.replace(new RegExp('-' + protectionString + 'ws-', "g"), ' ')
})
}

Expand All @@ -146,7 +149,8 @@ export const validateConfig = (config) => {
Object.hasOwn(config, 'tab_size') ||
Object.hasOwn(config, 'strict') ||
Object.hasOwn(config, 'ignore') ||
Object.hasOwn(config, 'trim'))
Object.hasOwn(config, 'trim') ||
Object.hasOwn(config, 'protection_string'))
if (config_empty) return CONFIG

let tab_size = config.tab_size
Expand Down
1 change: 1 addition & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ declare module 'htmlfy' {
strict?: boolean;
tab_size?: number;
trim?: string[];
protection_string?: string;
}

export type Config = Required<UserConfig>
Expand Down