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

Accept custom block tags map as an option #203

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
28 changes: 28 additions & 0 deletions packages/draft-js-export-html/src/__tests__/stateToHTML-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,32 @@ describe('stateToHTML', () => {
'a',
);
});

it('should support custom block tags map', () => {
let contentState = convertFromRaw(
{
entityMap: {},
blocks: [{
key: '33nh8',
text: 'text',
type: 'unstyled',
depth: 0,
inlineStyleRange: [],
entityRanges: [],
}],
},
);

expect(stateToHTML(contentState)).toBe(
'<p>text</p>',
);

expect(stateToHTML(contentState, {
blockTagsMap: {
unstyled: 'h1',
},
})).toBe(
'<h1>text</h1>',
);
});
});
14 changes: 11 additions & 3 deletions packages/draft-js-export-html/src/stateToHTML.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ type RenderConfig = {
style?: StyleDescr;
};

type HTMLTag = 'p' | 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6' | 'figure' | 'blockquote' | 'pre' | 'code' | 'li' | 'div';

type BlockTagsMap = {[blockType: string]: HTMLTag};
type BlockRenderer = (block: ContentBlock) => ?string;
type BlockRendererMap = {[blockType: string]: BlockRenderer};

Expand All @@ -42,6 +45,7 @@ type InlineStyleFn = (style: DraftInlineStyle) => ?RenderConfig;
type Options = {
inlineStyles?: StyleMap;
inlineStyleFn?: InlineStyleFn;
blockTagsMap?: BlockTagsMap;
blockRenderers?: BlockRendererMap;
blockStyleFn?: BlockStyleFn;
entityStyleFn?: EntityStyleFn;
Expand Down Expand Up @@ -125,7 +129,11 @@ const DATA_TO_ATTR = {

// The reason this returns an array is because a single block might get wrapped
// in two tags.
function getTags(blockType: string, defaultBlockTag): Array<string> {
function getTags(blockType: string, defaultBlockTag, blockTagsMap?: BlockTagsMap): Array<string> {
let customTag = blockTagsMap ? blockTagsMap[blockType] : null;
if (customTag) {
return [customTag];
}
switch (blockType) {
case BLOCK_TYPE.HEADER_ONE:
return ['h1'];
Expand Down Expand Up @@ -281,7 +289,7 @@ class MarkupGenerator {
}

writeStartTag(block, defaultBlockTag) {
let tags = getTags(block.getType(), defaultBlockTag);
let tags = getTags(block.getType(), defaultBlockTag, this.options.blockTagsMap);

let attrString;
if (this.options.blockStyleFn) {
Expand All @@ -306,7 +314,7 @@ class MarkupGenerator {
}

writeEndTag(block, defaultBlockTag) {
let tags = getTags(block.getType(), defaultBlockTag);
let tags = getTags(block.getType(), defaultBlockTag, this.options.blockTagsMap);
if (tags.length === 1) {
this.output.push(`</${tags[0]}>\n`);
} else {
Expand Down