@longform/async 0.0.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Matthew Quinn
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,196 @@
1
+ # Longform
2
+
3
+ <b class=keyword>Longform</b> is an easy to read markup language and transport format
4
+ for HTML fragments.
5
+
6
+
7
+ This repo contains the Longform language specification and some light weight Longform
8
+ parser implementations. Both the Longform language and parsers are a work in progress.
9
+
10
+
11
+ Read more about the Longform language in <a href=https://longform.occultist.dev>
12
+ the Longform Markup Language</a> specification document.
13
+
14
+
15
+ ## When to use not use these parsers
16
+
17
+ Longform boasts a number of interesting features allowing safe embedding of untrusted user
18
+ content. The sanitizer and templating specifications for these features are still
19
+ in development, as are the parser's features.
20
+
21
+
22
+ The Longform parsers also make heavy use of regular expressions. Currently these
23
+ have ReDos vulnerabilities in them and it will be a while before these issues are
24
+ addressed.
25
+
26
+
27
+ There a lot of uses for Longform without allowing untrusted user data and a lot of
28
+ the language features are mature. But the language and specification and these
29
+ parsers are not considered stable, keep that in mind if you decide to use them.
30
+
31
+
32
+ ## Install
33
+
34
+ ```
35
+ pnpm install @longform/async
36
+ deno install jsr:@longform/async
37
+
38
+ # the sync version is not the latest and is not on JSR.
39
+ # It is also missing features
40
+ pnpm install @longform/longform@=0.0.6
41
+ ```
42
+
43
+ ## Usage
44
+
45
+ ```typescript
46
+ import { longform } from '@longform/async';
47
+
48
+ const markup = `
49
+ @doctype:: html
50
+ html::
51
+ head::
52
+ title:: Example Longform
53
+ body::
54
+ h1:: Example Longform
55
+ #[fragment2]
56
+
57
+ #fragment1
58
+ div::
59
+ p::
60
+ This is a Longform fragment referenceable by it's identifier.
61
+
62
+ #fragment2
63
+ div::
64
+ p::
65
+ This fragment will be embedded in the fragment that references it.
66
+ `;
67
+
68
+ const result = await longform(markup);
69
+
70
+ // outputs the root fragment as HTML
71
+ console.log(result.root);
72
+
73
+ // outputs fragment1 as HTML
74
+ console.log(result.fragments.fragment1.html);
75
+ ```
76
+
77
+
78
+ ## Sync and Async Parsers
79
+
80
+ Several parsers are planned. The async parser is intended for uses of Longform
81
+ where custom directives might be used. The sync parser will be limited to
82
+ standard Longform features, so no custom directive support and will be usable
83
+ in places where code must be synchronous.
84
+
85
+
86
+ The roadmap is to get the async flavour feature complete, and then port
87
+ to the sync parser. Following that a direct to DOM parser will be developed
88
+ if it proves to be the more performant method of working with Longform in
89
+ the browser. Longform will have a well defined security model for in browser
90
+ use and support for the Trusted-Types API will be added if specific
91
+ considerations are required.
92
+
93
+
94
+ Also note there are many ways the editing capabilities of the Longform markup
95
+ languages could be implemented. The scope of these parsers is to eventually
96
+ allow data attributes be embedded in HTML elements which a client application
97
+ could use to complete the editing feature. Reading the resulting attributes
98
+ from the DOM, providing users with an editor and issuing the HTTP requests
99
+ is not in scope.
100
+
101
+
102
+ The following documentation applies to the `@longform/async` parser only.
103
+
104
+ See JSR's <a href=https://jsr.io/@longform/async/doc>docs page for @longform/async</a>
105
+ for current API documentation.
106
+
107
+
108
+ ## Status of Longform syntax features
109
+
110
+ Some syntax features are still in progress. Notably:
111
+
112
+ - Inline directives, ie `div:: @foo:bar`.
113
+ - Block args with blank lines work only if the blank like has some whitespace on it.
114
+ - Templates using the `##{foo-bar}` syntax to inline externally defined markup.
115
+ - Templating requires some extra escaping rules.
116
+ - Templating is currently a basic search and replace. This will later be
117
+ structurally aware and be restricted in some locations. The specification rules
118
+ will be expanded to explain these rules better so expect changes.
119
+
120
+
121
+ ## Status of standard directive support
122
+
123
+ Only supported or partially supported directives are documented here.
124
+ Most unsupported directives require the sanitizer rules be developed.
125
+
126
+
127
+ Also note that while the HTTP PATCH features are "supported" it is
128
+ likely that you would require other tooling to really make use of them.
129
+
130
+
131
+ ### `@id`
132
+
133
+ Supported.
134
+
135
+ ### `@lang`
136
+
137
+ Supported
138
+
139
+ ### `@dir`
140
+
141
+ Supported
142
+
143
+ ### `@doctype`
144
+
145
+ Supported
146
+
147
+ ### `@xml`
148
+
149
+ Supported
150
+
151
+ ### `@template`
152
+
153
+ Partially supported. The `processTemplate()` function can be used to
154
+ apply templating logic but not for nested Longform markup. Note that
155
+ this implementation will change as the sanitization features are
156
+ developed.
157
+
158
+
159
+ ## Custom directive support
160
+
161
+ The async parser supports custom directives with a number of hooks to work
162
+ for different locations in the document structure.
163
+
164
+ See the docs on the <a href=https://jsr.io/@longform/async/doc/~/Directive>
165
+ Directive</a> type for a description of the hooks available to directives.
166
+ Directives are passed to the Longform parser as an object.
167
+
168
+ ```typescript
169
+ const directives = {
170
+ 'foo:bar': {
171
+ meta: ({ inlineArgs }) => inlineArgs.trim(),
172
+ attr: ({ doc }) => doc.meta['foo:bar'],
173
+ render: ({ doc }) => doc.meta['foo:bar'],
174
+ },
175
+ 'example:md': {
176
+ render: ({ blockArgs }) => commonmark.render(commonmark.parse(blockArgs)),
177
+ },
178
+ };
179
+
180
+ const doc = `
181
+ @foo:bar:: Foo bar
182
+
183
+ html::
184
+ head::
185
+ title:: @foo:bar
186
+ meta::
187
+ [name=description]
188
+ [content=@foo:bar]
189
+ body::
190
+ @example:md::
191
+ # Example document using Markdown.
192
+ `;
193
+
194
+ const result = await longform(doc, { directives });
195
+ ```
196
+