bisheng-plugin-local-img
Advanced tools
Comparing version 1.0.5 to 1.0.6
@@ -6,30 +6,44 @@ 'use strict'; | ||
function isLocalImg(src) { | ||
return !/^https?:\/\/.*/i.test(src); | ||
return !!src && !/^https?:\/\/.*/i.test(src); | ||
} | ||
function findImgElment(jml) { | ||
if (JsonML.isElement(jml)) { | ||
const children = JsonML.isElement(jml) ? JsonML.getChildren(jml) : []; | ||
if (children.length < 2) { | ||
jml = convert(jml); | ||
} else { | ||
children.forEach(child => { | ||
findImgElment(child); | ||
}); | ||
} | ||
} | ||
} | ||
function convert(jml) { | ||
if (JsonML.getTagName(jml) === 'img') { | ||
console.log(jml); | ||
const src = JsonML.getAttribute(jml, 'src'); | ||
if (isLocalImg(src)) { | ||
return [ | ||
'pre', | ||
{ | ||
lang: '__react', | ||
}, | ||
['code', "ReactDOM.render(<img src={require('" + src + '\')} width="100px"/>, mountNode);'], | ||
]; | ||
} | ||
} | ||
return jml; | ||
} | ||
module.exports = (markdownData, config) => { | ||
const { content } = markdownData; | ||
JsonML.getChildren(content); | ||
// ignore customized content | ||
if (Array.isArray(content)) { | ||
markdownData.content = content.map(node => { | ||
console.log(node); | ||
const tagName = node[0]; | ||
const attr = node[1]; | ||
if (tagName === 'img' && attr && isLocalImg(attr.src)) { | ||
const src = attr.src; | ||
console.log(tagName, node); | ||
/* const code = node[2][1]; | ||
const processedCode = transformer(code, babelConfig && JSON.parse(babelConfig), noreact); | ||
return { | ||
__BISHENG_EMBEDED_CODE: true, | ||
code: processedCode, | ||
}; */ | ||
} | ||
return node; | ||
}); | ||
if (markdownData.meta && markdownData.meta.title == '文档标题 也作为菜单名称') { | ||
console.log('x'); | ||
if (Array.isArray(content)) { | ||
markdownData.content = findImgElment(content); | ||
} | ||
} | ||
return markdownData; | ||
}; |
{ | ||
"name": "bisheng-plugin-local-img", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"description": "change <img> to React code block if src is local file path", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
1964
43