包详细信息

svelte-parse-markup

bluwy113.3kMIT0.1.5

Parse Svelte markup without parsing the script or style tags

svelte, parse, markup, script

自述文件

svelte-parse-markup

Parse Svelte markup without parsing the script or style tags.

Usage

import { parse } from 'svelte-parse-markup'

const ast = parse('some svelte code')

console.log(ast.instance.content.body)
// output:
// [
//   {
//     type: 'Text',
//     start: <num>,
//     end: <num>,
//     raw: 'instance script content',
//     content: 'instance script content'
//   }
// ]

console.log(ast.module.content.body)
// output:
// [
//   {
//     type: 'Text',
//     start: <num>,
//     end: <num>,
//     raw: 'module script content',
//     content: 'module script content'
//   }
// ]

console.log(ast.css.content.styles)
console.log(ast.css.children)
// output:
// 'css content'
// [
//   {
//     type: 'Text',
//     start: <num>,
//     end: <num>,
//     raw: 'css content',
//     content: 'css content'
//   }
// ]

svelte-parse-markup skips parsing scripts and styles, and injects a Text node instead. Check out the Svelte REPL AST output tab to compare how the original AST would look like.

Sponsors

Sponsors

License

MIT

更新日志

Changelog

0.1.5 - 2024-06-04

  • Handle right-arrow symbol in generic attribute value
  • Handle self-closing script and style tags
  • Allow importing ESM from require()

0.1.4 - 2024-05-20

  • Correctly forward type of parse function (with overloads) from svelte/compiler
  • Add parent: null to generated Text node to satisfy Svelte 5 types

0.1.3 - 2024-05-18

  • Support Svelte 5

0.1.2 - 2023-06-23

  • Support Svelte 4

0.1.1 - 2022-08-02

  • Script and style tags don't need to start on the first char of line to be detected
  • Handle duplicate scripts and styles

0.1.0 - 2022-08-02

Initial release