Skip to content
Open
Show file tree
Hide file tree
Changes from all 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 js/__test__/blockTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ describe('addInlineStyleMarkup test suite', () => {
markup = addInlineStyleMarkup('ITALIC', 'test');
assert.equal(markup, '<em>test</em>');
markup = addInlineStyleMarkup('UNDERLINE', 'test');
assert.equal(markup, '<ins>test</ins>');
assert.equal(markup, '<u>test</u>');
markup = addInlineStyleMarkup('STRIKETHROUGH', 'test');
assert.equal(markup, '<del>test</del>');
markup = addInlineStyleMarkup('CODE', 'test');
Expand Down
20 changes: 10 additions & 10 deletions js/__test__/mainTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,24 @@ describe('draftToHtml test suite', () => {
assert.equal(output, result);

html = '<ol><li>1</li>\n<ol><li>2</li>\n</ol>\n<li>3</li>\n</ol>\n';
output = '<ol>\n<li>1</li>\n<ol>\n<li>2</li>\n</ol>\n<li>3</li>\n</ol>\n';
output = '<ol>\n<li>1<ol>\n<li>2</li>\n</ol>\n</li>\n<li>3</li>\n</ol>\n';
arrContentBlocks = convertFromHTML(html);
contentState = ContentState.createFromBlockArray(arrContentBlocks);
result = draftToHtml(convertToRaw(contentState));
assert.equal(output, result);

html = '<ol><li>1</li>\n<ol><li>2</li>\n<li>3</li>\n</ol>\n<li>4</li>\n</ol>\n';
output = '<ol>\n<li>1</li>\n<ol>\n<li>2</li>\n<li>'
+ '3</li>\n</ol>\n<li>4</li>\n</ol>\n';
output = '<ol>\n<li>1<ol>\n<li>2</li>\n<li>'
+ '3</li>\n</ol>\n</li>\n<li>4</li>\n</ol>\n';
arrContentBlocks = convertFromHTML(html);
contentState = ContentState.createFromBlockArray(arrContentBlocks);
result = draftToHtml(convertToRaw(contentState));
assert.equal(output, result);

html = '<ol><li>1</li>\n<ol><li>2</li>\n<ol><li>3</li>\n</ol>'
+ '\n</ol>\n<li>3</li>\n</ol>\n';
output = '<ol>\n<li>1</li>\n<ol>\n<li>2</li>\n<ol>\n<li>3'
+ '</li>\n</ol>\n</ol>\n<li>3</li>\n</ol>\n';
output = '<ol>\n<li>1<ol>\n<li>2<ol>\n<li>3'
+ '</li>\n</ol>\n</li>\n</ol>\n</li>\n<li>3</li>\n</ol>\n';
arrContentBlocks = convertFromHTML(html);
contentState = ContentState.createFromBlockArray(arrContentBlocks);
result = draftToHtml(convertToRaw(contentState));
Expand All @@ -72,24 +72,24 @@ describe('draftToHtml test suite', () => {
assert.equal(output, result);

html = '<ol><li>1</li>\n<ol><li>2</li>\n</ol>\n<li>3</li>\n</ol>\n';
output = '<ol>\n<li>1</li>\n<ol>\n<li>2</li>\n</ol>\n<li>3</li>\n</ol>\n';
output = '<ol>\n<li>1<ol>\n<li>2</li>\n</ol>\n</li>\n<li>3</li>\n</ol>\n';
arrContentBlocks = convertFromHTML(html);
contentState = ContentState.createFromBlockArray(arrContentBlocks);
result = draftToHtml(convertToRaw(contentState));
assert.equal(output, result);

html = '<ol><li>1</li>\n<ol><li>2</li>\n<li>3</li>\n</ol>\n<li>4</li>\n</ol>\n';
output = '<ol>\n<li>1</li>\n<ol>\n<li>2</li>\n<li>3'
+ '</li>\n</ol>\n<li>4</li>\n</ol>\n';
output = '<ol>\n<li>1<ol>\n<li>2</li>\n<li>3'
+ '</li>\n</ol>\n</li>\n<li>4</li>\n</ol>\n';
arrContentBlocks = convertFromHTML(html);
contentState = ContentState.createFromBlockArray(arrContentBlocks);
result = draftToHtml(convertToRaw(contentState));
assert.equal(output, result);

html = '<ol><li>1</li>\n<ol><li>2</li>\n<ol><li>3</li>\n</ol>'
+ '\n</ol>\n<li>3</li>\n</ol>\n';
output = '<ol>\n<li>1</li>\n<ol>\n<li>2</li>\n<ol>\n<li>3'
+ '</li>\n</ol>\n</ol>\n<li>3</li>\n</ol>\n';
output = '<ol>\n<li>1<ol>\n<li>2<ol>\n<li>3'
+ '</li>\n</ol>\n</li>\n</ol>\n</li>\n<li>3</li>\n</ol>\n';
arrContentBlocks = convertFromHTML(html);
contentState = ContentState.createFromBlockArray(arrContentBlocks);
result = draftToHtml(convertToRaw(contentState));
Expand Down
2 changes: 1 addition & 1 deletion js/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ export function addInlineStyleMarkup(style, content) {
} if (style === 'ITALIC') {
return `<em>${content}</em>`;
} if (style === 'UNDERLINE') {
return `<ins>${content}</ins>`;
return `<u>${content}</u>`;
} if (style === 'STRIKETHROUGH') {
return `<del>${content}</del>`;
} if (style === 'CODE') {
Expand Down
67 changes: 36 additions & 31 deletions js/list.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
import {
getBlockTag,
getBlockStyle,
getBlockInnerMarkup,
} from './block';
import { getBlockTag, getBlockStyle, getBlockInnerMarkup } from './block';

/**
* Function to check if a block is of type list.
*/
* Function to check if a block is of type list.
*/
export function isList(blockType) {
return (
blockType === 'unordered-list-item'
|| blockType === 'ordered-list-item'
blockType === 'unordered-list-item' || blockType === 'ordered-list-item'
);
}

/**
* Function will return html markup for a list block.
*/
* Function will return html markup for a list block.
*/
export function getListMarkup(
listBlocks,
entityMap,
Expand All @@ -36,13 +31,17 @@ export function getListMarkup(
listHtml.push(`<${getBlockTag(block.type)}>\n`);
} else if (previousBlock.depth === block.depth) {
if (nestedListBlock && nestedListBlock.length > 0) {
listHtml.push(getListMarkup(
nestedListBlock,
entityMap,
hashtagConfig,
directional,
customEntityTransform,
));
listHtml.splice(
listHtml.length - 1,
0,
getListMarkup(
nestedListBlock,
entityMap,
hashtagConfig,
directional,
customEntityTransform,
),
);
nestedListBlock = [];
}
} else {
Expand All @@ -59,24 +58,30 @@ export function getListMarkup(
listHtml.push(' dir = "auto"');
}
listHtml.push('>');
listHtml.push(getBlockInnerMarkup(
block,
entityMap,
hashtagConfig,
customEntityTransform,
));
listHtml.push(
getBlockInnerMarkup(
block,
entityMap,
hashtagConfig,
customEntityTransform,
),
);
listHtml.push('</li>\n');
previousBlock = block;
}
});
if (nestedListBlock && nestedListBlock.length > 0) {
listHtml.push(getListMarkup(
nestedListBlock,
entityMap,
hashtagConfig,
directional,
customEntityTransform,
));
listHtml.splice(
listHtml.length - 1,
0,
getListMarkup(
nestedListBlock,
entityMap,
hashtagConfig,
directional,
customEntityTransform,
),
);
}
listHtml.push(`</${getBlockTag(previousBlock.type)}>\n`);
return listHtml.join('');
Expand Down
17 changes: 10 additions & 7 deletions lib/draftjs-to-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
(global = global || self, global.draftjsToHtml = factory());
}(this, (function () { 'use strict';

/* @flow */

/**
* Utility function to execute callback for eack key->value pair.
*/
Expand Down Expand Up @@ -308,7 +310,7 @@
}

if (style === 'UNDERLINE') {
return "<ins>".concat(content, "</ins>");
return "<u>".concat(content, "</u>");
}

if (style === 'STRIKETHROUGH') {
Expand Down Expand Up @@ -617,15 +619,15 @@
}

/**
* Function to check if a block is of type list.
*/
* Function to check if a block is of type list.
*/

function isList(blockType) {
return blockType === 'unordered-list-item' || blockType === 'ordered-list-item';
}
/**
* Function will return html markup for a list block.
*/
* Function will return html markup for a list block.
*/

function getListMarkup(listBlocks, entityMap, hashtagConfig, directional, customEntityTransform) {
var listHtml = [];
Expand All @@ -641,7 +643,7 @@
listHtml.push("<".concat(getBlockTag(block.type), ">\n"));
} else if (previousBlock.depth === block.depth) {
if (nestedListBlock && nestedListBlock.length > 0) {
listHtml.push(getListMarkup(nestedListBlock, entityMap, hashtagConfig, directional, customEntityTransform));
listHtml.splice(listHtml.length - 1, 0, getListMarkup(nestedListBlock, entityMap, hashtagConfig, directional, customEntityTransform));
nestedListBlock = [];
}
} else {
Expand Down Expand Up @@ -669,13 +671,14 @@
});

if (nestedListBlock && nestedListBlock.length > 0) {
listHtml.push(getListMarkup(nestedListBlock, entityMap, hashtagConfig, directional, customEntityTransform));
listHtml.splice(listHtml.length - 1, 0, getListMarkup(nestedListBlock, entityMap, hashtagConfig, directional, customEntityTransform));
}

listHtml.push("</".concat(getBlockTag(previousBlock.type), ">\n"));
return listHtml.join('');
}

/* @flow */
/**
* The function will generate html markup for given draftjs editorContent.
*/
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Following is the list of conversions it supports:

2. Converts ordered and unordered list blocks with depths to nested structure of `<ul>, <ol>` and `<li>`.

3. Converts inline styles BOLD, ITALIC, UNDERLINE, STRIKETHROUGH, CODE, SUPERSCRIPT, SUBSCRIPT to corresponding HTML tags: `<strong>, <em>, <ins>, <code>, <sup>, <sub>`.
3. Converts inline styles BOLD, ITALIC, UNDERLINE, STRIKETHROUGH, CODE, SUPERSCRIPT, SUBSCRIPT to corresponding HTML tags: `<strong>, <em>, <u>, <code>, <sup>, <sub>`.

4. Converts inline styles color, background-color, font-size, font-family to a span tag with inline style details:
`<span style="color:xyz;font-size:xx">`. (The inline styles in JSON object should start with strings `color` or `font-size` like `color-red`, `color-green` or `fontsize-12`, `fontsize-20`).
Expand Down