1 | md-attr-parser
|
2 | ===========
|
3 |
|
4 | A node plugin to parse attributes (custom HTML attributes).
|
5 |
|
6 |
|
7 | ## Syntax
|
8 |
|
9 | The syntax is common :
|
10 |
|
11 | ```markdown
|
12 | {#thisIsAnId .thisIsAClass thisKey=thisValue}
|
13 |
|
14 | {thatKey="value" thisKey='thatValue'}
|
15 | ```
|
16 |
|
17 | ## Usage
|
18 |
|
19 | ```js
|
20 | const parseAttr = require('md-attr-parser');
|
21 |
|
22 |
|
23 | parseAttr('{ width=500px editable=true }');
|
24 |
|
25 | parseAttr('height=500px');
|
26 | ```
|
27 |
|
28 | The output is an object of the form :
|
29 | ```js
|
30 | {
|
31 | prop: { // Keep the key-value attribute
|
32 | class: undefined, // A list of class
|
33 | id: undefined, // The uniq id
|
34 | },
|
35 | eaten: '', // Every characters parsed
|
36 | }
|
37 | ```
|
38 |
|
39 | For example this code will output :
|
40 | ```js
|
41 | parseAttr('{ width=500px editable=true #unicorn .dangerous .cute }');
|
42 | ```
|
43 |
|
44 | ```js
|
45 | {
|
46 | prop: {
|
47 | class: ['dangerous', 'cute'],
|
48 | id: 'unicorn',
|
49 | width: '500px',
|
50 | editable: 'true',
|
51 | },
|
52 | eaten: '{ width=500px editable=true #unicorn .dangerous .cute }',
|
53 | }
|
54 | ```
|
55 |
|
56 | ### Advanced usage
|
57 |
|
58 | The parsing can start at a positive offset.
|
59 |
|
60 | ```js
|
61 | parseAttr('SYNTAX{ width=500px editable=true }', len('SYNTAX'));
|
62 | ```
|
63 |
|
64 | A configuration can also be specified, actualy, there is only one configuration option.
|
65 | The default value of key without value.
|
66 |
|
67 | ```js
|
68 | parseAttr('{ width=500px editable }', 0, {defaultValue: true});
|
69 | // or
|
70 | parseAttr('{ width=500px editable }', 0, {defaultValue: key => 'NO_VALUE_FOR_'+key.toUpperCase()});
|
71 | ```
|
72 |
|
73 | ## Licence
|
74 |
|
75 | MIT
|