Skip to content

Commit 1e81c6c

Browse files
author
CatHood0
committed
Chore: run prepublish
1 parent 1c9e6c4 commit 1e81c6c

12 files changed

+121
-21
lines changed

dist/colors.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
export declare function validateAndGetColor(colorString: string): string;
2-
export declare function colorToHex(color: string): string;
1+
export declare function validateAndGetColor(colorString: string): string | null;
2+
export declare function colorToHex(color: string): string | null;
33
export declare function rgbToHex(rgb: string): string;
44
export declare function rgbaToHex(rgba: string): string;
55
export declare function hslToHex(hsl: string): string;

dist/colors.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function colorToHex(color) {
4242
return hslaToHex(color);
4343
}
4444
else {
45-
throw new Error(`color format not supported: ${color}`);
45+
return null;
4646
}
4747
}
4848
/// Parses an RGB color string to a valid hexadecimal color string.

dist/default_html_to_operation.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,13 @@ class DefaultHtmlToOperations extends html_to_operation_1.HtmlOperations {
6767
(0, node_processor_1.processNode)(node, inlineAttributes, delta, true, this.customBlocks);
6868
});
6969
if (Object.keys(blockAttributes).length > 0) {
70+
let copyAttributes = {};
7071
for (const key in blockAttributes) {
71-
if (blockAttributes[key] === null) {
72-
delete blockAttributes[key];
72+
if (blockAttributes[key] !== null) {
73+
copyAttributes[key] = blockAttributes[key];
7374
}
7475
}
76+
blockAttributes = copyAttributes;
7577
delta.insert('\n', blockAttributes);
7678
}
7779
return delta.ops;
@@ -164,15 +166,17 @@ class DefaultHtmlToOperations extends html_to_operation_1.HtmlOperations {
164166
blockAttributes.header = parseInt(headerLevel.substring(1), 10);
165167
const nodes = element.childNodes;
166168
nodes.forEach((node) => {
167-
(0, node_processor_1.processNode)(node, attributes, delta, false, this.customBlocks);
169+
(0, node_processor_1.processNode)(node, attributes, delta, true, this.customBlocks, ['size']);
168170
});
169171
// Ensure a newline is added at the end of the header with the correct attributes
170172
if (Object.keys(blockAttributes).length > 0) {
173+
let copyAttributes = {};
171174
for (const key in blockAttributes) {
172-
if (blockAttributes[key] === null) {
173-
delete blockAttributes[key];
175+
if (blockAttributes[key] !== null) {
176+
copyAttributes[key] = blockAttributes[key];
174177
}
175178
}
179+
blockAttributes = copyAttributes;
176180
delta.insert('\n', blockAttributes);
177181
}
178182
return delta.ops;

dist/fontsize_parser.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ export declare const picasSizeMultiplier: number;
3535
* @param {number} [rootFontSizeRemMultiplier=16.0] - The font-size of the root element, used for `rem` units.
3636
* @returns {number} The equivalent value in pixels.
3737
*/
38-
export declare function parseToPx(value: string, fontSizeEmMultiplier?: number, rootFontSizeRemMultiplier?: number): number;
38+
export declare function parseToPx(value: string, fontSizeEmMultiplier?: number, rootFontSizeRemMultiplier?: number): number | null;

dist/fontsize_parser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,6 @@ function parseToPx(value, fontSizeEmMultiplier = 16.0, rootFontSizeRemMultiplier
6868
case 'rem':
6969
return number * rootFontSizeRemMultiplier;
7070
default:
71-
throw new Error(`Unit not supported: ${unit}`);
71+
return null;
7272
}
7373
}

dist/html_to_delta.d.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,22 @@ export declare class HtmlToDelta {
99
* Converts HTML tags to Delta operations based on defined rules.
1010
**/
1111
private htmlToOp;
12+
/** This is a list that must contains only the tag name
13+
* of the all HTML Nodes (something like: [`p`, `div`, `h1`]) that will be
14+
* inserted as plain text
15+
*
16+
* # Example
17+
* Assume that you want to ignore just HTML containers. Then just need
18+
* to do something like this:
19+
*
20+
* ```typescript
21+
* let containerBlackList: string[] = ['div', 'section', 'article'];
22+
*
23+
* let converter: HtmlToDelta = new HtmlToDelta(null, null, containerBlackList);
24+
* let delta = converter.convert(<your_html>);
25+
* ```
26+
**/
27+
private blackNodesList;
1228
/**
1329
* List of custom HTML parts to handle non-common HTML tags.
1430
*
@@ -27,9 +43,9 @@ export declare class HtmlToDelta {
2743
* })
2844
* ]);
2945
* ```
30-
*/
46+
**/
3147
private customBlocks;
32-
constructor(customBlocks?: CustomHtmlPart[], htmlToOperation?: HtmlOperations);
48+
constructor(customBlocks?: CustomHtmlPart[] | null, htmlToOperation?: HtmlOperations | null, blackNodesList?: string[]);
3349
/**
3450
* Converts an HTML string into Delta operations.
3551
*

dist/html_to_delta.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ const default_html_to_operation_1 = require("./default_html_to_operation");
1111
* Default converter for html to Delta
1212
**/
1313
class HtmlToDelta {
14-
constructor(customBlocks, htmlToOperation) {
14+
constructor(customBlocks, htmlToOperation, blackNodesList = []) {
1515
this.customBlocks = customBlocks || [];
1616
this.htmlToOp = htmlToOperation || new default_html_to_operation_1.DefaultHtmlToOperations();
1717
//automatically set custom block
1818
this.htmlToOp.setCustomBlocks(this.customBlocks);
19+
this.blackNodesList = blackNodesList;
1920
}
2021
/**
2122
* Converts an HTML string into Delta operations.
@@ -62,6 +63,10 @@ class HtmlToDelta {
6263
continue;
6364
}
6465
}
66+
if (this.blackNodesList.indexOf(node.localName) >= 0) {
67+
delta.push({ insert: node.text });
68+
continue;
69+
}
6570
const operations = this.htmlToOp.resolveCurrentElement(node, 0);
6671
operations.forEach((op) => {
6772
delta.push(op);

dist/html_utils.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,15 @@ function parseStyleAttribute(style) {
5757
break;
5858
case 'color':
5959
const color = (0, colors_1.validateAndGetColor)(value);
60-
attributes['color'] = color;
60+
if (color !== null) {
61+
attributes['color'] = color;
62+
}
6163
break;
6264
case 'background-color':
6365
const bgColor = (0, colors_1.validateAndGetColor)(value);
64-
attributes['background'] = bgColor;
66+
if (bgColor !== null) {
67+
attributes['background'] = bgColor;
68+
}
6569
break;
6670
case 'padding-left':
6771
case 'padding-right':
@@ -85,6 +89,8 @@ function parseStyleAttribute(style) {
8589
else {
8690
try {
8791
const size = (0, fontsize_parser_1.parseToPx)(value);
92+
if (size === null)
93+
break;
8894
if (size <= 10) {
8995
sizeToPass = 'small';
9096
}

dist/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {};

dist/index.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
const html_to_delta_1 = require("./html_to_delta");
4+
const delta = new html_to_delta_1.HtmlToDelta().convert('<p>Hello <strong>Word</strong>!<br></p>');
5+
delta.ops.forEach((op) => {
6+
console.log(JSON.stringify(op.insert) +
7+
(op.attributes ? JSON.stringify(op.attributes) : ''));
8+
});
9+
const delta2 = new html_to_delta_1.HtmlToDelta().convert('<p><span style="color:#F06292FF">This is just pink </span><br/><br/><span style="color:#4DD0E1FF">This is just blue</span><br></p>');
10+
console.log('\n');
11+
delta2.ops.forEach((op) => {
12+
console.log(JSON.stringify(op.insert) +
13+
(op.attributes ? JSON.stringify(op.attributes) : ''));
14+
});
15+
const delta3 = new html_to_delta_1.HtmlToDelta().convert('<p>This is a <a href="https://example.com">link</a> to example.com</p>');
16+
console.log('\n');
17+
delta3.ops.forEach((op) => {
18+
console.log(JSON.stringify(op.insert) +
19+
(op.attributes ? JSON.stringify(op.attributes) : ''));
20+
});
21+
const delta4 = new html_to_delta_1.HtmlToDelta().convert('<p style="font-size: 0.75em">This is a paragraph example</p>');
22+
console.log('\n');
23+
delta4.ops.forEach((op) => {
24+
console.log(JSON.stringify(op.insert) +
25+
(op.attributes ? JSON.stringify(op.attributes) : ''));
26+
});
27+
const delta5 = new html_to_delta_1.HtmlToDelta().convert('<p>This is an image: <img align="center" style="width: 50px;height: 250px;margin: 5px;" src="https://example.com/image.png"/> example</p>');
28+
console.log('\n');
29+
delta5.ops.forEach((op) => {
30+
console.log(JSON.stringify(op.insert) +
31+
(op.attributes ? JSON.stringify(op.attributes) : ''));
32+
});
33+
const delta6 = new html_to_delta_1.HtmlToDelta().convert('<ol><li>First <strong>item</strong><ul><li>SubItem <a href="https://www.google.com">1</a><ol><li>Sub 1.5</li></ol></li><li>SubItem 2</li></ul></li><li>Second item</li></ol>');
34+
console.log('\n');
35+
delta6.ops.forEach((op) => {
36+
console.log(JSON.stringify(op.insert) +
37+
(op.attributes ? JSON.stringify(op.attributes) : ''));
38+
});
39+
const delta7 = new html_to_delta_1.HtmlToDelta().convert('<h1>Example title <span style="font-size: 20px; color: var(--); background-color: #ffffffff">with an internal span</span></h1>');
40+
console.log('\n');
41+
delta7.ops.forEach((op) => {
42+
console.log(JSON.stringify(op.insert) +
43+
(op.attributes ? JSON.stringify(op.attributes) : ''));
44+
});
45+
const delta8 = new html_to_delta_1.HtmlToDelta(null, null, ['div']).convert('<h1>Example title <span style="font-size: 20px; color: var(--); background-color: #ffffffff">with an internal span</span></h1><div><p align="center">Paragraph ignored</p></div>');
46+
console.log('\n');
47+
delta8.ops.forEach((op) => {
48+
console.log(JSON.stringify(op.insert) +
49+
(op.attributes ? JSON.stringify(op.attributes) : ''));
50+
});

0 commit comments

Comments
 (0)