@prismicio/migrate 0.0.1
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 +202 -0
- package/README.md +96 -0
- package/dist/errors/PrismicRichTextError.cjs +6 -0
- package/dist/errors/PrismicRichTextError.cjs.map +1 -0
- package/dist/errors/PrismicRichTextError.d.ts +2 -0
- package/dist/errors/PrismicRichTextError.js +6 -0
- package/dist/errors/PrismicRichTextError.js.map +1 -0
- package/dist/errors/PrismicRichTextSerializerError.cjs +7 -0
- package/dist/errors/PrismicRichTextSerializerError.cjs.map +1 -0
- package/dist/errors/PrismicRichTextSerializerError.d.ts +3 -0
- package/dist/errors/PrismicRichTextSerializerError.js +7 -0
- package/dist/errors/PrismicRichTextSerializerError.js.map +1 -0
- package/dist/htmlAsRichText.cjs +11 -0
- package/dist/htmlAsRichText.cjs.map +1 -0
- package/dist/htmlAsRichText.d.ts +78 -0
- package/dist/htmlAsRichText.js +11 -0
- package/dist/htmlAsRichText.js.map +1 -0
- package/dist/index.cjs +9 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/RichTextFieldBuilder.cjs +115 -0
- package/dist/lib/RichTextFieldBuilder.cjs.map +1 -0
- package/dist/lib/RichTextFieldBuilder.d.ts +35 -0
- package/dist/lib/RichTextFieldBuilder.js +115 -0
- package/dist/lib/RichTextFieldBuilder.js.map +1 -0
- package/dist/lib/filterRichTextField.cjs +87 -0
- package/dist/lib/filterRichTextField.cjs.map +1 -0
- package/dist/lib/filterRichTextField.d.ts +10 -0
- package/dist/lib/filterRichTextField.js +87 -0
- package/dist/lib/filterRichTextField.js.map +1 -0
- package/dist/lib/hastSerializerHelpers.cjs +95 -0
- package/dist/lib/hastSerializerHelpers.cjs.map +1 -0
- package/dist/lib/hastSerializerHelpers.d.ts +28 -0
- package/dist/lib/hastSerializerHelpers.js +95 -0
- package/dist/lib/hastSerializerHelpers.js.map +1 -0
- package/dist/lib/hastToRichText.cjs +164 -0
- package/dist/lib/hastToRichText.cjs.map +1 -0
- package/dist/lib/hastToRichText.d.ts +30 -0
- package/dist/lib/hastToRichText.js +164 -0
- package/dist/lib/hastToRichText.js.map +1 -0
- package/dist/lib/rehypeRichText.cjs +59 -0
- package/dist/lib/rehypeRichText.cjs.map +1 -0
- package/dist/lib/rehypeRichText.d.ts +65 -0
- package/dist/lib/rehypeRichText.js +59 -0
- package/dist/lib/rehypeRichText.js.map +1 -0
- package/dist/types.d.ts +57 -0
- package/package.json +95 -0
- package/src/errors/PrismicRichTextError.ts +1 -0
- package/src/errors/PrismicRichTextSerializerError.ts +3 -0
- package/src/htmlAsRichText.ts +117 -0
- package/src/index.ts +7 -0
- package/src/lib/RichTextFieldBuilder.ts +164 -0
- package/src/lib/filterRichTextField.ts +158 -0
- package/src/lib/hastSerializerHelpers.ts +151 -0
- package/src/lib/hastToRichText.ts +294 -0
- package/src/lib/rehypeRichText.ts +185 -0
- package/src/types.ts +72 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# @prismicio/migrate
|
|
2
|
+
|
|
3
|
+
[![npm version][npm-version-src]][npm-version-href]
|
|
4
|
+
[![npm downloads][npm-downloads-src]][npm-downloads-href]
|
|
5
|
+
[![Github Actions CI][github-actions-ci-src]][github-actions-ci-href]
|
|
6
|
+
[![Codecov][codecov-src]][codecov-href]
|
|
7
|
+
[![Conventional Commits][conventional-commits-src]][conventional-commits-href]
|
|
8
|
+
[![License][license-src]][license-href]
|
|
9
|
+
|
|
10
|
+
Set of helpers to migrate your data to [Prismic][prismic].
|
|
11
|
+
|
|
12
|
+
<!--
|
|
13
|
+
|
|
14
|
+
TODO: Create a small list of package features:
|
|
15
|
+
|
|
16
|
+
- 🤔 A useful feature;
|
|
17
|
+
- 🥴 Another useful feature;
|
|
18
|
+
- 🙃 A final useful feature.
|
|
19
|
+
|
|
20
|
+
Non-breaking space: are here on purpose to fix emoji rendering on certain systems.
|
|
21
|
+
|
|
22
|
+
-->
|
|
23
|
+
|
|
24
|
+
## Install
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
npm install @prismicio/migrate
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Documentation
|
|
31
|
+
|
|
32
|
+
To discover what's new on this package check out [the changelog][changelog]. For full documentation, visit the [official Prismic documentation][prismic-docs].
|
|
33
|
+
|
|
34
|
+
## Contributing
|
|
35
|
+
|
|
36
|
+
Whether you're helping us fix bugs, improve the docs, or spread the word, we'd love to have you as part of the Prismic developer community!
|
|
37
|
+
|
|
38
|
+
**Asking a question**: [Open a new topic][forum-question] on our community forum explaining what you want to achieve / your question. Our support team will get back to you shortly.
|
|
39
|
+
|
|
40
|
+
**Reporting a bug**: [Open an issue][repo-bug-report] explaining your application's setup and the bug you're encountering.
|
|
41
|
+
|
|
42
|
+
**Suggesting an improvement**: [Open an issue][repo-feature-request] explaining your improvement or feature so we can discuss and learn more.
|
|
43
|
+
|
|
44
|
+
**Submitting code changes**: For small fixes, feel free to [open a pull request][repo-pull-requests] with a description of your changes. For large changes, please first [open an issue][repo-feature-request] so we can discuss if and how the changes should be implemented.
|
|
45
|
+
|
|
46
|
+
For more clarity on this project and its structure you can also check out the detailed [CONTRIBUTING.md][contributing] document.
|
|
47
|
+
|
|
48
|
+
## License
|
|
49
|
+
|
|
50
|
+
```
|
|
51
|
+
Copyright 2013-2024 Prismic <contact@prismic.io> (https://prismic.io)
|
|
52
|
+
|
|
53
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
54
|
+
you may not use this file except in compliance with the License.
|
|
55
|
+
You may obtain a copy of the License at
|
|
56
|
+
|
|
57
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
58
|
+
|
|
59
|
+
Unless required by applicable law or agreed to in writing, software
|
|
60
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
61
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
62
|
+
See the License for the specific language governing permissions and
|
|
63
|
+
limitations under the License.
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
<!-- Links -->
|
|
67
|
+
|
|
68
|
+
[prismic]: https://prismic.io
|
|
69
|
+
|
|
70
|
+
<!-- TODO: Replace link with a more useful one if available -->
|
|
71
|
+
|
|
72
|
+
[prismic-docs]: https://prismic.io/docs
|
|
73
|
+
[changelog]: ./CHANGELOG.md
|
|
74
|
+
[contributing]: ./CONTRIBUTING.md
|
|
75
|
+
|
|
76
|
+
<!-- TODO: Replace link with a more useful one if available -->
|
|
77
|
+
|
|
78
|
+
[forum-question]: https://community.prismic.io
|
|
79
|
+
[repo-bug-report]: https://github.com/prismicio/prismic-migrate/issues/new?assignees=&labels=bug&template=bug_report.md&title=
|
|
80
|
+
[repo-feature-request]: https://github.com/prismicio/prismic-migrate/issues/new?assignees=&labels=enhancement&template=feature_request.md&title=
|
|
81
|
+
[repo-pull-requests]: https://github.com/prismicio/prismic-migrate/pulls
|
|
82
|
+
|
|
83
|
+
<!-- Badges -->
|
|
84
|
+
|
|
85
|
+
[npm-version-src]: https://img.shields.io/npm/v/@prismicio/migrate/latest.svg
|
|
86
|
+
[npm-version-href]: https://npmjs.com/package/@prismicio/migrate
|
|
87
|
+
[npm-downloads-src]: https://img.shields.io/npm/dm/@prismicio/migrate.svg
|
|
88
|
+
[npm-downloads-href]: https://npmjs.com/package/@prismicio/migrate
|
|
89
|
+
[github-actions-ci-src]: https://github.com/prismicio/prismic-migrate/workflows/ci/badge.svg
|
|
90
|
+
[github-actions-ci-href]: https://github.com/prismicio/prismic-migrate/actions?query=workflow%3Aci
|
|
91
|
+
[codecov-src]: https://img.shields.io/codecov/c/github/prismicio/prismic-migrate.svg
|
|
92
|
+
[codecov-href]: https://codecov.io/gh/prismicio/prismic-migrate
|
|
93
|
+
[conventional-commits-src]: https://img.shields.io/badge/Conventional%20Commits-1.0.0-%23FE5196?logo=conventionalcommits&logoColor=white
|
|
94
|
+
[conventional-commits-href]: https://conventionalcommits.org
|
|
95
|
+
[license-src]: https://img.shields.io/npm/l/@prismicio/migrate.svg
|
|
96
|
+
[license-href]: https://npmjs.com/package/@prismicio/migrate
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PrismicRichTextError.cjs","sources":["../../../src/errors/PrismicRichTextError.ts"],"sourcesContent":["export class PrismicRichTextError extends Error {}\n"],"names":[],"mappings":";;AAAM,MAAO,6BAA6B,MAAK;AAAG;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PrismicRichTextError.js","sources":["../../../src/errors/PrismicRichTextError.ts"],"sourcesContent":["export class PrismicRichTextError extends Error {}\n"],"names":[],"mappings":"AAAM,MAAO,6BAA6B,MAAK;AAAG;"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const PrismicRichTextError = require("./PrismicRichTextError.cjs");
|
|
4
|
+
class PrismicRichTextSerializerError extends PrismicRichTextError.PrismicRichTextError {
|
|
5
|
+
}
|
|
6
|
+
exports.PrismicRichTextSerializerError = PrismicRichTextSerializerError;
|
|
7
|
+
//# sourceMappingURL=PrismicRichTextSerializerError.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PrismicRichTextSerializerError.cjs","sources":["../../../src/errors/PrismicRichTextSerializerError.ts"],"sourcesContent":["import { PrismicRichTextError } from \"./PrismicRichTextError\"\n\nexport class PrismicRichTextSerializerError extends PrismicRichTextError {}\n"],"names":["PrismicRichTextError"],"mappings":";;;AAEM,MAAO,uCAAuCA,qBAAAA,qBAAoB;AAAG;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PrismicRichTextSerializerError.js","sources":["../../../src/errors/PrismicRichTextSerializerError.ts"],"sourcesContent":["import { PrismicRichTextError } from \"./PrismicRichTextError\"\n\nexport class PrismicRichTextSerializerError extends PrismicRichTextError {}\n"],"names":[],"mappings":";AAEM,MAAO,uCAAuC,qBAAoB;AAAG;"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const rehypeParse = require("rehype-parse");
|
|
4
|
+
const unified = require("unified");
|
|
5
|
+
const rehypeRichText = require("./lib/rehypeRichText.cjs");
|
|
6
|
+
const htmlAsRichText = (html, config) => {
|
|
7
|
+
const { result, messages } = unified.unified().use(rehypeParse, { emitParseErrors: true, missingDoctype: 0 }).use(rehypeRichText.rehypeRichText, config).processSync(html);
|
|
8
|
+
return { result, warnings: messages.map((message) => message.toString()) };
|
|
9
|
+
};
|
|
10
|
+
exports.htmlAsRichText = htmlAsRichText;
|
|
11
|
+
//# sourceMappingURL=htmlAsRichText.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"htmlAsRichText.cjs","sources":["../../src/htmlAsRichText.ts"],"sourcesContent":["// Used for TSDocs only.\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\nimport type { HTMLRichTextMapSerializer, asHTML } from \"@prismicio/client\"\nimport type {\n\tRTInlineNode,\n\tRTNode,\n\tRichTextField,\n\tRichTextNodeTypes,\n} from \"@prismicio/client\"\nimport type { Element } from \"hast\"\nimport rehypeParse from \"rehype-parse\"\nimport { unified } from \"unified\"\n\nimport type { RTPartialInlineNode } from \"./lib/RichTextFieldBuilder\"\nimport type { RehypeRichTextConfig } from \"./lib/rehypeRichText\"\nimport { rehypeRichText } from \"./lib/rehypeRichText\"\n\n/**\n * A shorthand definition for {@link RichTextHTMLMapSerializer} rich text node\n * types.\n *\n * @remarks\n * The `label` rich text node type is not available as is. Use an object\n * containing your label name to convert to label nodes instead. For example:\n * `u: { label: \"underline\" }`\n * @remarks\n * The `span` rich text node type is not available as it is not relevant in the\n * context of going from HTML to Prismic rich text.\n */\nexport type RichTextHTMLMapSerializerShorthand =\n\t| Exclude<RichTextNodeTypes, \"label\" | \"span\">\n\t| { label: string }\n\n/**\n * The payload provided to a {@link RichTextHTMLMapSerializerFunction}.\n */\ntype RichTextHTMLMapSerializerFunctionPayload = {\n\t/**\n\t * The hast {@link Element} node to serialize.\n\t */\n\tnode: Element\n\n\t/**\n\t * Additional context information to help with the serialization.\n\t */\n\tcontext: {\n\t\t/**\n\t\t * The list type of the last list node encountered if any.\n\t\t */\n\t\tlistType?: \"group-list-item\" | \"group-o-list-item\"\n\t}\n}\n\n/**\n * Serializes a hast {@link Element} node to a\n * {@link RichTextHTMLMapSerializerShorthand} or a rich text node.\n *\n * @remarks\n * Serializing to a rich text node directly is not recommended and is only\n * available as an escape hatch. Prefer returning a\n * {@link RichTextHTMLMapSerializerShorthand} instead.\n */\nexport type RichTextHTMLMapSerializerFunction = (\n\tpayload: RichTextHTMLMapSerializerFunctionPayload,\n) =>\n\t| RichTextHTMLMapSerializerShorthand\n\t| RTNode\n\t| RTInlineNode\n\t| RTPartialInlineNode\n\t| undefined\n\n/**\n * Serializes a hast {@link Element} node matching the given HTML tag name or CSS\n * selector to a Prismic rich text node.\n *\n * @remarks\n * This serializer is used to serialize HTML to Prismic rich text. When\n * serializing Prismic rich text to HTML with {@link asHTML}, use the\n * {@link HTMLRichTextMapSerializer} type instead.\n */\nexport type RichTextHTMLMapSerializer = Record<\n\tstring,\n\tRichTextHTMLMapSerializerShorthand | RichTextHTMLMapSerializerFunction\n>\n\n/**\n * Configuration that determines the output of {@link htmlAsRichText}.\n */\nexport type HTMLAsRichTextConfig = RehypeRichTextConfig\n\n/**\n * The return type of {@link htmlAsRichText}.\n */\ntype HTMLAsRichTextReturnType = {\n\tresult: RichTextField\n\twarnings: string[]\n}\n\n/**\n * Converts an HTML string to a rich text field.\n *\n * @param html - An HTML string.\n * @param config - Configuration that determines the output the function.\n *\n * @returns `html` as rich text.\n */\nexport const htmlAsRichText = (\n\thtml: string,\n\tconfig?: HTMLAsRichTextConfig,\n): HTMLAsRichTextReturnType => {\n\tconst { result, messages } = unified()\n\t\t.use(rehypeParse, { emitParseErrors: true, missingDoctype: 0 })\n\t\t.use(rehypeRichText, config)\n\t\t.processSync(html)\n\n\treturn { result, warnings: messages.map((message) => message.toString()) }\n}\n"],"names":["unified","rehypeRichText"],"mappings":";;;;;AA0Ga,MAAA,iBAAiB,CAC7B,MACA,WAC6B;AACvB,QAAA,EAAE,QAAQ,aAAaA,QAAAA,QAC3B,EAAA,IAAI,aAAa,EAAE,iBAAiB,MAAM,gBAAgB,EAAA,CAAG,EAC7D,IAAIC,eAAAA,gBAAgB,MAAM,EAC1B,YAAY,IAAI;AAEX,SAAA,EAAE,QAAQ,UAAU,SAAS,IAAI,CAAC,YAAY,QAAQ,SAAU,CAAA;AACxE;;"}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { RTInlineNode, RTNode, RichTextField, RichTextNodeTypes } from "@prismicio/client";
|
|
2
|
+
import type { Element } from "hast";
|
|
3
|
+
import type { RTPartialInlineNode } from "./lib/RichTextFieldBuilder";
|
|
4
|
+
import type { RehypeRichTextConfig } from "./lib/rehypeRichText";
|
|
5
|
+
/**
|
|
6
|
+
* A shorthand definition for {@link RichTextHTMLMapSerializer} rich text node
|
|
7
|
+
* types.
|
|
8
|
+
*
|
|
9
|
+
* @remarks
|
|
10
|
+
* The `label` rich text node type is not available as is. Use an object
|
|
11
|
+
* containing your label name to convert to label nodes instead. For example:
|
|
12
|
+
* `u: { label: "underline" }`
|
|
13
|
+
* @remarks
|
|
14
|
+
* The `span` rich text node type is not available as it is not relevant in the
|
|
15
|
+
* context of going from HTML to Prismic rich text.
|
|
16
|
+
*/
|
|
17
|
+
export type RichTextHTMLMapSerializerShorthand = Exclude<RichTextNodeTypes, "label" | "span"> | {
|
|
18
|
+
label: string;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* The payload provided to a {@link RichTextHTMLMapSerializerFunction}.
|
|
22
|
+
*/
|
|
23
|
+
type RichTextHTMLMapSerializerFunctionPayload = {
|
|
24
|
+
/**
|
|
25
|
+
* The hast {@link Element} node to serialize.
|
|
26
|
+
*/
|
|
27
|
+
node: Element;
|
|
28
|
+
/**
|
|
29
|
+
* Additional context information to help with the serialization.
|
|
30
|
+
*/
|
|
31
|
+
context: {
|
|
32
|
+
/**
|
|
33
|
+
* The list type of the last list node encountered if any.
|
|
34
|
+
*/
|
|
35
|
+
listType?: "group-list-item" | "group-o-list-item";
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* Serializes a hast {@link Element} node to a
|
|
40
|
+
* {@link RichTextHTMLMapSerializerShorthand} or a rich text node.
|
|
41
|
+
*
|
|
42
|
+
* @remarks
|
|
43
|
+
* Serializing to a rich text node directly is not recommended and is only
|
|
44
|
+
* available as an escape hatch. Prefer returning a
|
|
45
|
+
* {@link RichTextHTMLMapSerializerShorthand} instead.
|
|
46
|
+
*/
|
|
47
|
+
export type RichTextHTMLMapSerializerFunction = (payload: RichTextHTMLMapSerializerFunctionPayload) => RichTextHTMLMapSerializerShorthand | RTNode | RTInlineNode | RTPartialInlineNode | undefined;
|
|
48
|
+
/**
|
|
49
|
+
* Serializes a hast {@link Element} node matching the given HTML tag name or CSS
|
|
50
|
+
* selector to a Prismic rich text node.
|
|
51
|
+
*
|
|
52
|
+
* @remarks
|
|
53
|
+
* This serializer is used to serialize HTML to Prismic rich text. When
|
|
54
|
+
* serializing Prismic rich text to HTML with {@link asHTML}, use the
|
|
55
|
+
* {@link HTMLRichTextMapSerializer} type instead.
|
|
56
|
+
*/
|
|
57
|
+
export type RichTextHTMLMapSerializer = Record<string, RichTextHTMLMapSerializerShorthand | RichTextHTMLMapSerializerFunction>;
|
|
58
|
+
/**
|
|
59
|
+
* Configuration that determines the output of {@link htmlAsRichText}.
|
|
60
|
+
*/
|
|
61
|
+
export type HTMLAsRichTextConfig = RehypeRichTextConfig;
|
|
62
|
+
/**
|
|
63
|
+
* The return type of {@link htmlAsRichText}.
|
|
64
|
+
*/
|
|
65
|
+
type HTMLAsRichTextReturnType = {
|
|
66
|
+
result: RichTextField;
|
|
67
|
+
warnings: string[];
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* Converts an HTML string to a rich text field.
|
|
71
|
+
*
|
|
72
|
+
* @param html - An HTML string.
|
|
73
|
+
* @param config - Configuration that determines the output the function.
|
|
74
|
+
*
|
|
75
|
+
* @returns `html` as rich text.
|
|
76
|
+
*/
|
|
77
|
+
export declare const htmlAsRichText: (html: string, config?: HTMLAsRichTextConfig) => HTMLAsRichTextReturnType;
|
|
78
|
+
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import rehypeParse from "rehype-parse";
|
|
2
|
+
import { unified } from "unified";
|
|
3
|
+
import { rehypeRichText } from "./lib/rehypeRichText.js";
|
|
4
|
+
const htmlAsRichText = (html, config) => {
|
|
5
|
+
const { result, messages } = unified().use(rehypeParse, { emitParseErrors: true, missingDoctype: 0 }).use(rehypeRichText, config).processSync(html);
|
|
6
|
+
return { result, warnings: messages.map((message) => message.toString()) };
|
|
7
|
+
};
|
|
8
|
+
export {
|
|
9
|
+
htmlAsRichText
|
|
10
|
+
};
|
|
11
|
+
//# sourceMappingURL=htmlAsRichText.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"htmlAsRichText.js","sources":["../../src/htmlAsRichText.ts"],"sourcesContent":["// Used for TSDocs only.\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\nimport type { HTMLRichTextMapSerializer, asHTML } from \"@prismicio/client\"\nimport type {\n\tRTInlineNode,\n\tRTNode,\n\tRichTextField,\n\tRichTextNodeTypes,\n} from \"@prismicio/client\"\nimport type { Element } from \"hast\"\nimport rehypeParse from \"rehype-parse\"\nimport { unified } from \"unified\"\n\nimport type { RTPartialInlineNode } from \"./lib/RichTextFieldBuilder\"\nimport type { RehypeRichTextConfig } from \"./lib/rehypeRichText\"\nimport { rehypeRichText } from \"./lib/rehypeRichText\"\n\n/**\n * A shorthand definition for {@link RichTextHTMLMapSerializer} rich text node\n * types.\n *\n * @remarks\n * The `label` rich text node type is not available as is. Use an object\n * containing your label name to convert to label nodes instead. For example:\n * `u: { label: \"underline\" }`\n * @remarks\n * The `span` rich text node type is not available as it is not relevant in the\n * context of going from HTML to Prismic rich text.\n */\nexport type RichTextHTMLMapSerializerShorthand =\n\t| Exclude<RichTextNodeTypes, \"label\" | \"span\">\n\t| { label: string }\n\n/**\n * The payload provided to a {@link RichTextHTMLMapSerializerFunction}.\n */\ntype RichTextHTMLMapSerializerFunctionPayload = {\n\t/**\n\t * The hast {@link Element} node to serialize.\n\t */\n\tnode: Element\n\n\t/**\n\t * Additional context information to help with the serialization.\n\t */\n\tcontext: {\n\t\t/**\n\t\t * The list type of the last list node encountered if any.\n\t\t */\n\t\tlistType?: \"group-list-item\" | \"group-o-list-item\"\n\t}\n}\n\n/**\n * Serializes a hast {@link Element} node to a\n * {@link RichTextHTMLMapSerializerShorthand} or a rich text node.\n *\n * @remarks\n * Serializing to a rich text node directly is not recommended and is only\n * available as an escape hatch. Prefer returning a\n * {@link RichTextHTMLMapSerializerShorthand} instead.\n */\nexport type RichTextHTMLMapSerializerFunction = (\n\tpayload: RichTextHTMLMapSerializerFunctionPayload,\n) =>\n\t| RichTextHTMLMapSerializerShorthand\n\t| RTNode\n\t| RTInlineNode\n\t| RTPartialInlineNode\n\t| undefined\n\n/**\n * Serializes a hast {@link Element} node matching the given HTML tag name or CSS\n * selector to a Prismic rich text node.\n *\n * @remarks\n * This serializer is used to serialize HTML to Prismic rich text. When\n * serializing Prismic rich text to HTML with {@link asHTML}, use the\n * {@link HTMLRichTextMapSerializer} type instead.\n */\nexport type RichTextHTMLMapSerializer = Record<\n\tstring,\n\tRichTextHTMLMapSerializerShorthand | RichTextHTMLMapSerializerFunction\n>\n\n/**\n * Configuration that determines the output of {@link htmlAsRichText}.\n */\nexport type HTMLAsRichTextConfig = RehypeRichTextConfig\n\n/**\n * The return type of {@link htmlAsRichText}.\n */\ntype HTMLAsRichTextReturnType = {\n\tresult: RichTextField\n\twarnings: string[]\n}\n\n/**\n * Converts an HTML string to a rich text field.\n *\n * @param html - An HTML string.\n * @param config - Configuration that determines the output the function.\n *\n * @returns `html` as rich text.\n */\nexport const htmlAsRichText = (\n\thtml: string,\n\tconfig?: HTMLAsRichTextConfig,\n): HTMLAsRichTextReturnType => {\n\tconst { result, messages } = unified()\n\t\t.use(rehypeParse, { emitParseErrors: true, missingDoctype: 0 })\n\t\t.use(rehypeRichText, config)\n\t\t.processSync(html)\n\n\treturn { result, warnings: messages.map((message) => message.toString()) }\n}\n"],"names":[],"mappings":";;;AA0Ga,MAAA,iBAAiB,CAC7B,MACA,WAC6B;AACvB,QAAA,EAAE,QAAQ,aAAa,QAC3B,EAAA,IAAI,aAAa,EAAE,iBAAiB,MAAM,gBAAgB,EAAA,CAAG,EAC7D,IAAI,gBAAgB,MAAM,EAC1B,YAAY,IAAI;AAEX,SAAA,EAAE,QAAQ,UAAU,SAAS,IAAI,CAAC,YAAY,QAAQ,SAAU,CAAA;AACxE;"}
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const htmlAsRichText = require("./htmlAsRichText.cjs");
|
|
4
|
+
const PrismicRichTextError = require("./errors/PrismicRichTextError.cjs");
|
|
5
|
+
const PrismicRichTextSerializerError = require("./errors/PrismicRichTextSerializerError.cjs");
|
|
6
|
+
exports.htmlAsRichText = htmlAsRichText.htmlAsRichText;
|
|
7
|
+
exports.PrismicRichTextError = PrismicRichTextError.PrismicRichTextError;
|
|
8
|
+
exports.PrismicRichTextSerializerError = PrismicRichTextSerializerError.PrismicRichTextSerializerError;
|
|
9
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { htmlAsRichText } from "./htmlAsRichText";
|
|
2
|
+
export type { RichTextHTMLMapSerializer } from "./types";
|
|
3
|
+
export { PrismicRichTextError } from "./errors/PrismicRichTextError";
|
|
4
|
+
export { PrismicRichTextSerializerError } from "./errors/PrismicRichTextSerializerError";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { htmlAsRichText } from "./htmlAsRichText.js";
|
|
2
|
+
import { PrismicRichTextError } from "./errors/PrismicRichTextError.js";
|
|
3
|
+
import { PrismicRichTextSerializerError } from "./errors/PrismicRichTextSerializerError.js";
|
|
4
|
+
export {
|
|
5
|
+
PrismicRichTextError,
|
|
6
|
+
PrismicRichTextSerializerError,
|
|
7
|
+
htmlAsRichText
|
|
8
|
+
};
|
|
9
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;"}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __typeError = (msg) => {
|
|
3
|
+
throw TypeError(msg);
|
|
4
|
+
};
|
|
5
|
+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
6
|
+
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
7
|
+
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
8
|
+
var _nodes, _RichTextFieldBuilder_instances, last_get;
|
|
9
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
10
|
+
const PrismicRichTextError = require("../errors/PrismicRichTextError.cjs");
|
|
11
|
+
class RichTextFieldBuilder {
|
|
12
|
+
constructor() {
|
|
13
|
+
__privateAdd(this, _RichTextFieldBuilder_instances);
|
|
14
|
+
__privateAdd(this, _nodes, []);
|
|
15
|
+
}
|
|
16
|
+
appendNode(node) {
|
|
17
|
+
this.cleanupLast();
|
|
18
|
+
__privateGet(this, _nodes).push(node);
|
|
19
|
+
}
|
|
20
|
+
appendTextNode(type, direction = "ltr") {
|
|
21
|
+
this.appendNode({
|
|
22
|
+
type,
|
|
23
|
+
text: "",
|
|
24
|
+
spans: [],
|
|
25
|
+
direction
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
appendSpan(spanOrPartialSpan, length) {
|
|
29
|
+
var _a;
|
|
30
|
+
if (!this.isTextNode(__privateGet(this, _RichTextFieldBuilder_instances, last_get))) {
|
|
31
|
+
throw new PrismicRichTextError.PrismicRichTextError("Cannot add span to non-text last node");
|
|
32
|
+
}
|
|
33
|
+
let span;
|
|
34
|
+
if ("start" in spanOrPartialSpan && "end" in spanOrPartialSpan) {
|
|
35
|
+
span = spanOrPartialSpan;
|
|
36
|
+
} else {
|
|
37
|
+
const lastLength = __privateGet(this, _RichTextFieldBuilder_instances, last_get).text.length;
|
|
38
|
+
span = {
|
|
39
|
+
...spanOrPartialSpan,
|
|
40
|
+
start: lastLength,
|
|
41
|
+
end: lastLength + length
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
let lastIdenticalSpanIndex = -1;
|
|
45
|
+
for (let i = __privateGet(this, _RichTextFieldBuilder_instances, last_get).spans.length - 1; i >= 0; i--) {
|
|
46
|
+
const lastSpan = __privateGet(this, _RichTextFieldBuilder_instances, last_get).spans[i];
|
|
47
|
+
if (span.type === "strong" || span.type === "em") {
|
|
48
|
+
if (lastSpan.type === span.type) {
|
|
49
|
+
lastIdenticalSpanIndex = i;
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
} else if (span.type === "label") {
|
|
53
|
+
if (lastSpan.type === "label" && lastSpan.data.label === span.data.label) {
|
|
54
|
+
lastIdenticalSpanIndex = i;
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
} else if (span.type === "hyperlink") {
|
|
58
|
+
if (lastSpan.type === "hyperlink") {
|
|
59
|
+
const isSameLink = Object.entries(span.data).every(([key, value]) => lastSpan.data[key] === value);
|
|
60
|
+
if (isSameLink) {
|
|
61
|
+
lastIdenticalSpanIndex = i;
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (span.start === span.end) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
if (((_a = __privateGet(this, _RichTextFieldBuilder_instances, last_get).spans[lastIdenticalSpanIndex]) == null ? void 0 : _a.end) === span.start) {
|
|
71
|
+
__privateGet(this, _RichTextFieldBuilder_instances, last_get).spans[lastIdenticalSpanIndex].end = span.end;
|
|
72
|
+
} else {
|
|
73
|
+
__privateGet(this, _RichTextFieldBuilder_instances, last_get).spans.push(span);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
appendText(text) {
|
|
77
|
+
if (!this.isTextNode(__privateGet(this, _RichTextFieldBuilder_instances, last_get))) {
|
|
78
|
+
throw new PrismicRichTextError.PrismicRichTextError("Cannot append text to non-text last node");
|
|
79
|
+
}
|
|
80
|
+
if (__privateGet(this, _RichTextFieldBuilder_instances, last_get).text) {
|
|
81
|
+
__privateGet(this, _RichTextFieldBuilder_instances, last_get).text += text;
|
|
82
|
+
} else {
|
|
83
|
+
__privateGet(this, _RichTextFieldBuilder_instances, last_get).text = text.trimStart();
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
build() {
|
|
87
|
+
this.cleanupLast();
|
|
88
|
+
return __privateGet(this, _nodes);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Cleans up the current last node.
|
|
92
|
+
*/
|
|
93
|
+
cleanupLast() {
|
|
94
|
+
if (this.isTextNode(__privateGet(this, _RichTextFieldBuilder_instances, last_get))) {
|
|
95
|
+
__privateGet(this, _RichTextFieldBuilder_instances, last_get).text = __privateGet(this, _RichTextFieldBuilder_instances, last_get).text.trimEnd();
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Determines if a node is a text node.
|
|
100
|
+
*
|
|
101
|
+
* @param node - rich text node to check.
|
|
102
|
+
*
|
|
103
|
+
* @returns `true` if `node` is a text node, `false` otherwise.
|
|
104
|
+
*/
|
|
105
|
+
isTextNode(node) {
|
|
106
|
+
return !!node && node.type !== "image" && node.type !== "embed";
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
_nodes = new WeakMap();
|
|
110
|
+
_RichTextFieldBuilder_instances = new WeakSet();
|
|
111
|
+
last_get = function() {
|
|
112
|
+
return __privateGet(this, _nodes)[__privateGet(this, _nodes).length - 1];
|
|
113
|
+
};
|
|
114
|
+
exports.RichTextFieldBuilder = RichTextFieldBuilder;
|
|
115
|
+
//# sourceMappingURL=RichTextFieldBuilder.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RichTextFieldBuilder.cjs","sources":["../../../src/lib/RichTextFieldBuilder.ts"],"sourcesContent":["import type {\n\tRTInlineNode,\n\tRTLinkNode,\n\tRTNode,\n\tRTTextNode,\n\tRichTextField,\n} from \"@prismicio/client\"\n\nimport { PrismicRichTextError } from \"../errors/PrismicRichTextError\"\n\n/**\n * Omits keys from a type, distributing the operation over a union.\n *\n * Taken from the `type-fest` package.\n *\n * @see https://github.com/sindresorhus/type-fest/blob/8a45ba048767aaffcebc7d190172d814a739feb0/source/distributed-omit.d.ts\n */\ntype DistributedOmit<\n\tObjectType,\n\tKeyType extends keyof ObjectType,\n> = ObjectType extends unknown ? Omit<ObjectType, KeyType> : never\n\n/**\n * An inline node without its `start` and `end` properties.\n */\nexport type RTPartialInlineNode = DistributedOmit<RTInlineNode, \"start\" | \"end\">\n\nexport class RichTextFieldBuilder {\n\t#nodes: RTNode[] = []\n\n\tget #last(): RTNode | undefined {\n\t\treturn this.#nodes[this.#nodes.length - 1]\n\t}\n\n\tappendNode(node: RTNode): void {\n\t\tthis.cleanupLast()\n\n\t\tthis.#nodes.push(node)\n\t}\n\n\tappendTextNode(\n\t\ttype: RTTextNode[\"type\"],\n\t\tdirection: \"ltr\" | \"rtl\" = \"ltr\",\n\t): void {\n\t\tthis.appendNode({\n\t\t\ttype,\n\t\t\ttext: \"\",\n\t\t\tspans: [],\n\t\t\tdirection,\n\t\t})\n\t}\n\n\tappendSpan(span: RTInlineNode): void\n\tappendSpan(partialSpan: RTPartialInlineNode, length: number): void\n\tappendSpan(\n\t\tspanOrPartialSpan: RTInlineNode | RTPartialInlineNode,\n\t\tlength?: number,\n\t): void {\n\t\tif (!this.isTextNode(this.#last)) {\n\t\t\tthrow new PrismicRichTextError(\"Cannot add span to non-text last node\")\n\t\t}\n\n\t\tlet span: RTInlineNode\n\t\tif (\"start\" in spanOrPartialSpan && \"end\" in spanOrPartialSpan) {\n\t\t\tspan = spanOrPartialSpan\n\t\t} else {\n\t\t\tconst lastLength = this.#last.text.length\n\n\t\t\tspan = {\n\t\t\t\t...spanOrPartialSpan,\n\t\t\t\tstart: lastLength,\n\t\t\t\tend: lastLength + length!,\n\t\t\t}\n\t\t}\n\n\t\tlet lastIdenticalSpanIndex = -1\n\t\tfor (let i = this.#last.spans.length - 1; i >= 0; i--) {\n\t\t\tconst lastSpan = this.#last.spans[i]\n\n\t\t\tif (span.type === \"strong\" || span.type === \"em\") {\n\t\t\t\tif (lastSpan.type === span.type) {\n\t\t\t\t\tlastIdenticalSpanIndex = i\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t} else if (span.type === \"label\") {\n\t\t\t\tif (\n\t\t\t\t\tlastSpan.type === \"label\" &&\n\t\t\t\t\tlastSpan.data.label === span.data.label\n\t\t\t\t) {\n\t\t\t\t\tlastIdenticalSpanIndex = i\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t} else if (span.type === \"hyperlink\") {\n\t\t\t\tif (lastSpan.type === \"hyperlink\") {\n\t\t\t\t\t// Check that all data fields are the same. Since it's not a\n\t\t\t\t\t// deep equal operation, this doesn't guarantee a perfect\n\t\t\t\t\t// match with content relationship.\n\t\t\t\t\tconst isSameLink = (\n\t\t\t\t\t\tObject.entries(span.data) as [keyof RTLinkNode[\"data\"], unknown][]\n\t\t\t\t\t).every(([key, value]) => lastSpan.data[key] === value)\n\n\t\t\t\t\tif (isSameLink) {\n\t\t\t\t\t\tlastIdenticalSpanIndex = i\n\t\t\t\t\t\tbreak\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// Ignore empty spans.\n\t\tif (span.start === span.end) {\n\t\t\treturn\n\t\t}\n\n\t\t// Prefer to extend the last span of the same type end\n\t\t// position if it ends at the start of the new span.\n\t\tif (this.#last.spans[lastIdenticalSpanIndex]?.end === span.start) {\n\t\t\tthis.#last.spans[lastIdenticalSpanIndex].end = span.end\n\t\t} else {\n\t\t\tthis.#last.spans.push(span)\n\t\t}\n\t}\n\n\tappendText(text: string): void {\n\t\tif (!this.isTextNode(this.#last)) {\n\t\t\tthrow new PrismicRichTextError(\"Cannot append text to non-text last node\")\n\t\t}\n\n\t\tif (this.#last.text) {\n\t\t\tthis.#last.text += text\n\t\t} else {\n\t\t\tthis.#last.text = text.trimStart()\n\t\t}\n\t}\n\n\tbuild(): RichTextField {\n\t\t// Ensure that the last text node is trimmed.\n\t\tthis.cleanupLast()\n\n\t\t// Because `RichTextField` is defined as a non-empty\n\t\t// array, we have to cast `RTNode[]` to `RichTextField`.\n\t\treturn this.#nodes as RichTextField\n\t}\n\n\t/**\n\t * Cleans up the current last node.\n\t */\n\tprivate cleanupLast(): void {\n\t\tif (this.isTextNode(this.#last)) {\n\t\t\tthis.#last.text = this.#last.text.trimEnd()\n\t\t}\n\t}\n\n\t/**\n\t * Determines if a node is a text node.\n\t *\n\t * @param node - rich text node to check.\n\t *\n\t * @returns `true` if `node` is a text node, `false` otherwise.\n\t */\n\tprivate isTextNode(node?: RTNode): node is RTTextNode {\n\t\treturn !!node && node.type !== \"image\" && node.type !== \"embed\"\n\t}\n}\n"],"names":["PrismicRichTextError"],"mappings":";;;;;;;;;;MA2Ba,qBAAoB;AAAA;;AAChC,+BAAmB,CAAA;AAAA;AAAA,EAMnB,WAAW,MAAY;AACtB,SAAK,YAAW;AAEX,uBAAA,QAAO,KAAK,IAAI;AAAA,EACtB;AAAA,EAEA,eACC,MACA,YAA2B,OAAK;AAEhC,SAAK,WAAW;AAAA,MACf;AAAA,MACA,MAAM;AAAA,MACN,OAAO,CAAE;AAAA,MACT;AAAA,IAAA,CACA;AAAA,EACF;AAAA,EAIA,WACC,mBACA,QAAe;;AAEf,QAAI,CAAC,KAAK,WAAW,mBAAK,0CAAK,GAAG;AAC3B,YAAA,IAAIA,qBAAAA,qBAAqB,uCAAuC;AAAA,IACvE;AAEI,QAAA;AACA,QAAA,WAAW,qBAAqB,SAAS,mBAAmB;AACxD,aAAA;AAAA,IAAA,OACD;AACA,YAAA,aAAa,mBAAK,2CAAM,KAAK;AAE5B,aAAA;AAAA,QACN,GAAG;AAAA,QACH,OAAO;AAAA,QACP,KAAK,aAAa;AAAA,MAAA;AAAA,IAEpB;AAEA,QAAI,yBAAyB;AACpB,aAAA,IAAI,mBAAK,2CAAM,MAAM,SAAS,GAAG,KAAK,GAAG,KAAK;AACtD,YAAM,WAAW,mBAAK,2CAAM,MAAM,CAAC;AAEnC,UAAI,KAAK,SAAS,YAAY,KAAK,SAAS,MAAM;AAC7C,YAAA,SAAS,SAAS,KAAK,MAAM;AACP,mCAAA;AACzB;AAAA,QACD;AAAA,MAAA,WACU,KAAK,SAAS,SAAS;AAEhC,YAAA,SAAS,SAAS,WAClB,SAAS,KAAK,UAAU,KAAK,KAAK,OACjC;AACwB,mCAAA;AACzB;AAAA,QACD;AAAA,MAAA,WACU,KAAK,SAAS,aAAa;AACjC,YAAA,SAAS,SAAS,aAAa;AAIlC,gBAAM,aACL,OAAO,QAAQ,KAAK,IAAI,EACvB,MAAM,CAAC,CAAC,KAAK,KAAK,MAAM,SAAS,KAAK,GAAG,MAAM,KAAK;AAEtD,cAAI,YAAY;AACU,qCAAA;AACzB;AAAA,UACD;AAAA,QACD;AAAA,MACD;AAAA,IACD;AAGI,QAAA,KAAK,UAAU,KAAK,KAAK;AAC5B;AAAA,IACD;AAIA,UAAI,wBAAK,2CAAM,MAAM,sBAAsB,MAAvC,mBAA0C,SAAQ,KAAK,OAAO;AACjE,yBAAK,2CAAM,MAAM,sBAAsB,EAAE,MAAM,KAAK;AAAA,IAAA,OAC9C;AACD,yBAAA,2CAAM,MAAM,KAAK,IAAI;AAAA,IAC3B;AAAA,EACD;AAAA,EAEA,WAAW,MAAY;AACtB,QAAI,CAAC,KAAK,WAAW,mBAAK,0CAAK,GAAG;AAC3B,YAAA,IAAIA,qBAAAA,qBAAqB,0CAA0C;AAAA,IAC1E;AAEI,QAAA,mBAAK,2CAAM,MAAM;AACpB,yBAAK,2CAAM,QAAQ;AAAA,IAAA,OACb;AACD,yBAAA,2CAAM,OAAO,KAAK,UAAS;AAAA,IACjC;AAAA,EACD;AAAA,EAEA,QAAK;AAEJ,SAAK,YAAW;AAIhB,WAAO,mBAAK;AAAA,EACb;AAAA;AAAA;AAAA;AAAA,EAKQ,cAAW;AAClB,QAAI,KAAK,WAAW,mBAAK,0CAAK,GAAG;AAChC,yBAAK,2CAAM,OAAO,mBAAK,2CAAM,KAAK;IACnC;AAAA,EACD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASQ,WAAW,MAAa;AAC/B,WAAO,CAAC,CAAC,QAAQ,KAAK,SAAS,WAAW,KAAK,SAAS;AAAA,EACzD;AACA;AAvIA;;AAEI,WAAK,WAAA;AACR,SAAO,mBAAK,QAAO,mBAAK,QAAO,SAAS,CAAC;AAC1C;;"}
|