Skip to content

Commit fa89404

Browse files
authored
Feature/#26 template expr parser (#87)
* first draft of expression parser * fix preserve whitespace, fix unstable formatting result when node was broken after first text element * fix #78 * v0.8.5 * simplify formatter rules * align first text node * reset example * remove unused var * reimplement formatting rule for condition comments * remove invalid comments * v0.8.6 * clean attribute expression * update readme * update readme * Merge branch 'master' into feature/#26_temlate_expr_parser # Conflicts: # packages/prettyhtml-formatter/package-lock.json * fix typo * fix merge, cleanup comments * dont enable attribute clean up by default
1 parent be57b17 commit fa89404

14 files changed

Lines changed: 7110 additions & 39 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Opinionated general formatter for your Angular, Vue, Svelte or pure HTML5 templa
3535
- [prettyhtml-sort-attributes](/packages/prettyhtml-sort-attributes) Sort attributes alphabetically.
3636
- [prettyhtml-quick](/packages/prettyhtml-quick) Formats your changed files based on Git.
3737
- [webparser](/packages/webparser) Optimized HTML parser for formatters
38+
- [expression-parser](/packages/expression-parser) Framework agnostic template expression parser.
3839
- [rehype-webparser](/packages/rehype-webparser) Adapter between HTML parser and rehype.
3940
- [rehype-minify-whitespace](/packages/rehype-minify-whitespace) Collapse whitespace.
4041
- [hast-util-from-parse](/packages/hast-util-from-webparser) Transform [webparser](/packages/webparser) AST to HAST.
@@ -50,7 +51,7 @@ Adding this flag before a tag will preserve from whitespace and/or attribute wra
5051
<div></div>
5152
```
5253

53-
2. Preserve only from whitespace processing. This excludes no indentation.
54+
2. Preserve only from whitespace processing. This excludes indentation.
5455

5556
```html
5657
<!--prettyhtml-preserve-whitespace-->
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# @starptech/expression-parser
2+
3+
Framework agnostic template expression parser
4+
5+
## Installation
6+
7+
```
8+
npm install --save @starptech/expression-parser
9+
```
10+
11+
## Usage
12+
13+
This example shows how we parse HTML
14+
15+
```js
16+
const parse = require('@starptech/expression-parser')
17+
const result = parse(`{ a + /<g></g> b }`, { brackets: ['{', '}'] })
18+
```
19+
20+
## Representation
21+
22+
- `unescape`: Indentify a template marker as escaped. This information is useful to understand why a marker was skipped
23+
- `expressions`: A list of template expressions
24+
- `start`: Start position
25+
- `end`: End position
26+
- `text`: The content of the expression
27+
28+
```json
29+
{
30+
"unescape": "",
31+
"expressions": [
32+
{
33+
"start": 1,
34+
"end": 19,
35+
"text": " a + /<g></g> b "
36+
}
37+
]
38+
}
39+
```
40+
41+
## Details
42+
43+
There may be more than one expression as part of one attribute value or text node, or only one replacing the entire value or node.
44+
45+
When used as the whole attribute value, there's no need to enclose the expression inside quotes, even if the expression contains whitespace.
46+
47+
Single and double quotes can be nested inside the expression.
48+
49+
To emit opening (left) brackets as literal text wherever an opening bracket is expected, the bracket must be prefixed with a backslash (the JS escape char '\'). This character is preserved in the output, but the parser will add a replace property for the attribute or node containing the escaped bracket, whose value is the bracket itself.
50+
51+
## Credits
52+
53+
The parser is a modificated version of [Riot](https://github.com/riot/parser) template expression parser.

0 commit comments

Comments
 (0)