thx / gogocode

GoGoCode is a transformer for JavaScript/Typescript/HTML based on AST but providing a more intuitive API.
MIT License
5.47k stars 417 forks source link

Bug Report From Playground:html的注释转换失败 #205

Closed wuzebin closed 1 year ago

wuzebin commented 1 year ago

转换前的源代码

<!-- 
    <div>文案</div>
-->

transform script

function transform(fileInfo, api, options) {
  const $ = api.gogocode
  const source = fileInfo.source
  const ast = $(source, { parseOptions: { language: 'html' } })
  ast.replace('<!-- $$$0 -->', '<!-- 注释 $$$0 -->')
  return ast.generate()
}

期望得到的结果

<!-- 注释
    <div>文案</div>
-->

实际转换后的结果

<!-- 注释 $$$0 -->

207835714-1eda4442-0eb7-4402-8495-db2da94b4d77

wuzebin commented 1 year ago

通过ast.find,然后在each中自行处理

ast.find('<!---->').each(item=>{
    item.node.content.value.content = '注释' + item.node.content.value.content
})