Skip to content

Commit

Permalink
fix(content-transform): special tags need special content transform
Browse files Browse the repository at this point in the history
if pre tags is change to codeView, then it failed to tranform pre content with special settings.
  • Loading branch information
thorecaspersen committed Jun 10, 2019
1 parent 6d7f958 commit 74415b5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/flotFyrHastCompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ module.exports = function flotFyrHastCompiler(options) {
}
);
}

return {
jsx: result,
allUsedHtmlToJsxTags: uniqElementNames
Expand Down
16 changes: 12 additions & 4 deletions lib/hastUtilToJsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ function renderChildren(ctx, parent) {
function renderElement(ctx, node) {
const tagName = node.tagName;
let elementName;
// oldElementName is for remeber tag name. Forexsampel we change pre to codeView,
// then the pre content still need the to be transformed
// (see the if for textarea, style and pre)
let oldElementName = tagName;
// this is for keeping track of the change tags. so we can make a special import for that later
// forexsampel h1 to headline, then we need to import the headline component in the end file.
let UsedHtmlToJsxTag = "";
// If the element is set to be tranformted in the options to some react component.
if (tagName in ctx) {
Expand All @@ -50,7 +56,9 @@ function renderElement(ctx, node) {
}
let renderedChildrenData = renderChildren(
ctx,
elementName === "template" ? node.content : node
elementName === "template" || oldElementName === "template"
? node.content
: node
);
let renderedChildren = renderedChildrenData.allNotes;
let props = hastPropertiesToJsxProps(node.properties, elementName);
Expand All @@ -71,16 +79,16 @@ function renderElement(ctx, node) {
return `<${elementName}${renderedProps} />`;
}

if (elementName === "textarea") {
if (elementName === "textarea" || oldElementName === "textarea") {
const dangerousChild = JSON.stringify(renderedChildren);
return `<textarea${renderedProps} defaultValue=${dangerousChild} />`;
}
if (elementName === "style") {
if (elementName === "style" || oldElementName === "style") {
const dangerousChild = JSON.stringify(renderedChildren);
return `<style${renderedProps} dangerouslySetInnerHTML={{__html: ${dangerousChild} }} />`;
}

if (elementName === "pre") {
if (elementName === "pre" || oldElementName === "pre") {
renderedChildren = renderedChildren.replace(
/( {2,}|\n|\t)/g,
whitespace => {
Expand Down

0 comments on commit 74415b5

Please sign in to comment.