Skip to content

Commit

Permalink
refactor(umi-plugin): compatible with close symbol missing tag (#18)
Browse files Browse the repository at this point in the history
related PR: #24
  • Loading branch information
PeachScript committed Dec 9, 2019
1 parent cac02cf commit ff71be4
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions packages/umi-plugin-father-doc/src/transformer/remark/jsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,25 @@ import { Node } from 'unist';
import visit from 'unist-util-visit';
import visitParents from 'unist-util-visit-parents';

const SINGLE_TAGS_EXPS = [
'area',
'base',
'br',
'col',
'command',
'embed',
'hr',
'img',
'input',
'keygen',
'link',
'meta',
'param',
'source',
'track',
'wbr',
].map(tag => new RegExp(`<(${tag}[^>]*?)>`, 'g'));

function hasSubClassName(className: string[], subCls: string) {
return (className || []).find(cls => cls.indexOf(subCls) > -1);
}
Expand Down Expand Up @@ -53,6 +72,20 @@ const rawVisitor = (node, i, parent) => {
if (COMMENT_EXP.test(node.value)) {
parent.children.splice(i, 1);
}

// convert all self-closing HTML tag
// see also: https://github.com/umijs/umi/blob/master/packages/umi-build-dev/src/htmlToJSX.js#L118
if (!node.previewer) {
SINGLE_TAGS_EXPS.forEach(function(regex) {
node.value = node.value.replace(regex, function(_, str) {
if (str.endsWith('/')) {
return `<${str}>`;
} else {
return `<${str} />`;
}
});
});
}
}

export default () => (ast: Node) => {
Expand Down

0 comments on commit ff71be4

Please sign in to comment.