npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

wxml-parse

v0.2.3

Published

wxml -> ast -> wxml,wxml美化或者压缩皆可

Downloads

82

Readme

wxml-parse

使用 原生js 对 wxml 字符串进行解析,生成相应的 AST,然后再转换成 wxml 字符串。

效果图如下: 左图为美化后的代码,右边为原图

avatar

生成为 wxml 时,

  • text 文本的内容将不会进行换行,防止内容掉落。
  • 子元素有多个时,会进行换行展示,
  • 单个子元素并且为纯文本时,将不会换行展示
  • wxs 标签会检查 module 属性 和 内容的导出
  • 标签未正确闭合时,将会抛出错误

安装

npm i wxml-parse

使用

import { parse, generate, traverse, NODE_TYPES } from "wxml-parse";

// <wxs>var a = '<wxs>'</wxs>
const ast = parse(
   `<view wx:if=" sda " bindtap="{{ sda }}">123</view><wxs module="sad">var a = '<wxs>';module.exports=123</wxs>` +
    `<!--wxml-->
  <template name="staffName">
    <view>
      FirstName: {{firstName}}, LastName: {{lastName}}
    </view>
  </template>

  <template is="staffName" data="{{...staffA}}"></template>
  ` +
    `
  <!--\${node.comment}-->

  <view wx:if="{{xsda}}" class="sda {{sda}}" id="123" name="sadasdsadasdsadasdsadasdsadasdsadasdsadasdsadasdsadasdsadasdsadasdsadasdsadasdsadasdsadasdsadasdsadasdsadasdsadasd\\"\\"" style="<view>view style</view>">

  <text id="123" name="sadasdsadasdsa" />

  <text>sads撒大苏打阿萨大<text>qqw的撒</text></text>
  </view>`
);

traverse(ast, {
  [NODE_TYPES.TEXT](node) {
    console.log(node);
  },
});

const str = generate(ast as any, {
  compress: false, // 是否压缩代码
  // maxWidth: 120,默认值
});

字符串str等于一下内容:

<view wx:if="{{ sda }}" bindtap="sda">123</view>
<wxs module="sad">var a = '<wxs>';module.exports=123</wxs>
<!-- wxml -->
<template name="staffName">
	<view>FirstName: {{firstName}}, LastName: {{lastName}}</view>
</template>
<template is="staffName" data="{{ ...staffA }}"></template>
<!-- ${node.comment} -->
<view
	wx:if="{{ xsda }}"
	class="sda {{ sda }}"
	id="123"
	name="sadasdsadasdsadasdsadasdsadasdsadasdsadasdsadasdsadasdsadasdsadasdsadasdsadasdsadasdsadasdsadasdsadasdsadasdsadasd\"\""
	style="<view>view style</view>"
>
	<text id="123" name="sadasdsadasdsa" />
	<text>sads撒大苏打阿萨大<text>qqw的撒</text></text>
</view>

相关API

基本类型文件

1. parse

将 wxml 转换为 AST

const parse: (str: string) => RootNode

2. traverse

遍历 AST,需要自定义属性时,可使用该方法

// 1 3 8 均来自于 NODE_TYPES 类型
declare const fn: {
    1(node: ElementNode): void;
    3(node: TextNode): void;
    8(node: CommentNode): void;
    0(node: RootNode): void;
};
declare type VisitorFn = Partial<typeof fn>;
export declare function traverse(node: BaseNode | BaseNode[] | RootNode, visitor: VisitorFn): void;

3. generate

根据 AST 重新转换为 wxml字符串,可压缩或者美化

export declare type ConfigType = {
  compress?: boolean; // false 表示美化, true表示压缩
  maxWidth?: number; // 默认值 120, 元素属性大于该值时,将会换行
};
export declare const generate: (node: BaseNode | BaseNode[] | RootNode, config?: ConfigType) => any;