@takeshape/util 8.16.0 → 8.20.2

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.
@@ -0,0 +1,8 @@
1
+ import { getImageUrl as imageUrl } from '@takeshape/routing';
2
+ export function getImageUrl(path, defaultImageConfig, imageBaseUrl, config) {
3
+ return imageUrl(path, { ...defaultImageConfig,
4
+ ...config
5
+ }, {
6
+ baseUrl: imageBaseUrl
7
+ });
8
+ }
@@ -0,0 +1,27 @@
1
+ /*
2
+ * The intention is to move all this to the client package in the end, but right now it's here
3
+ * to be shared between the draftjs / mdx implementations, which span the client / server differently.
4
+ */
5
+ import Prism from 'prismjs';
6
+ import 'prismjs/components/prism-javascript';
7
+ import 'prismjs/components/prism-markup';
8
+ import 'prismjs/components/prism-markup-templating';
9
+ import 'prismjs/components/prism-css';
10
+ import 'prismjs/components/prism-bash';
11
+ import 'prismjs/components/prism-django';
12
+ import 'prismjs/components/prism-twig';
13
+ import 'prismjs/components/prism-yaml';
14
+ import 'prismjs/components/prism-graphql';
15
+ import 'prismjs/components/prism-sass';
16
+ import 'prismjs/components/prism-scss';
17
+ import 'prismjs/components/prism-markdown';
18
+ import 'prismjs/components/prism-json';
19
+ import escape from 'lodash/escape';
20
+ export const highlightCode = (code, lang) => {
21
+ if (lang) {
22
+ const syntax = Prism.languages[lang];
23
+ return Prism.highlight(code, syntax, lang);
24
+ }
25
+
26
+ return escape(code);
27
+ };
@@ -0,0 +1,97 @@
1
+ /*
2
+ * The intention is to move all this to the client package in the end, but right now it's here
3
+ * to be shared between the draftjs / mdx implementations, which span the client / server differently.
4
+ */
5
+ import classnames from 'classnames';
6
+ import { highlightCode } from './highlight-code';
7
+ import escape from 'lodash/escape';
8
+ import { getImageUrl } from '@takeshape/routing';
9
+ export function getApplyPrefix(prefix) {
10
+ return className => className ? prefix + className : '';
11
+ }
12
+ export function attrs(obj) {
13
+ const attrStrings = [];
14
+ const attrNames = Object.keys(obj);
15
+ attrNames.sort();
16
+
17
+ for (const attrName of attrNames) {
18
+ const value = obj[attrName];
19
+
20
+ if (value) {
21
+ attrStrings.push(`${attrName}="${escape(value)}"`);
22
+ }
23
+ }
24
+
25
+ return attrStrings.length ? ' ' + attrStrings.join(' ') : '';
26
+ }
27
+ export function imageTemplate(applyPrefix, data) {
28
+ var _data$link;
29
+
30
+ const {
31
+ caption,
32
+ credit,
33
+ alignment,
34
+ size,
35
+ asset,
36
+ imageParams
37
+ } = data;
38
+
39
+ if (!asset) {
40
+ return '';
41
+ }
42
+
43
+ const imageUrl = getImageUrl(asset.path, imageParams);
44
+ let figCaption = '';
45
+
46
+ if (caption || credit) {
47
+ const htmlCaption = caption ? `<span class="${applyPrefix('caption')}">${caption}</span>` : '';
48
+ const htmlCredit = credit ? `<span class="${applyPrefix('credit')}">${credit}</span>` : '';
49
+ figCaption = `<figcaption>${htmlCaption} ${htmlCredit}</figcaption>`;
50
+ }
51
+
52
+ const classes = classnames(applyPrefix(alignment), applyPrefix(size));
53
+ const classAttr = classes ? ` class="${classes}"` : '';
54
+ const imgAttrs = attrs({
55
+ alt: asset.description,
56
+ title: asset.title
57
+ });
58
+ let image = `<img${imgAttrs} src="${imageUrl}"/>`;
59
+
60
+ if ((_data$link = data.link) !== null && _data$link !== void 0 && _data$link.url) {
61
+ const target = data.link.external ? ' target="blank" rel="noopener noreferrer"' : '';
62
+ image = `<a href="${data.link.url}"${target}>${image}</a>`;
63
+ }
64
+
65
+ return `<figure${classAttr}>${image}${figCaption}</figure>`;
66
+ }
67
+
68
+ function renderMdx(tag, attributes, data, children) {
69
+ const tagWithAttributes = `${tag} ${attributes.map(attr => `${attr}="${escape(data[attr])}"`).join(' ')}`;
70
+
71
+ if (children) {
72
+ return `<${tagWithAttributes}>
73
+ ${children.replace(/[\n\r]+$/, '')}
74
+ </${tag}>`;
75
+ }
76
+
77
+ return `<${tagWithAttributes}/>`;
78
+ }
79
+
80
+ export function imageTemplateMdx(applyPrefix, data) {
81
+ return renderMdx(applyPrefix('Image'), ['id', 'caption', 'credit', 'link', 'linkisexternal', 'alignment', 'size', 'src'], data);
82
+ }
83
+ export function oembedTemplate(applyPrefix, data) {
84
+ return `<div class="${applyPrefix('oembed')}">${data.html}</div>`;
85
+ }
86
+ export function oembedTemplateMdx(applyPrefix, data) {
87
+ return renderMdx(applyPrefix('Oembed'), ['url', 'author_name', 'author_url', 'width', 'height', 'type', 'cache_age', 'provider_name', 'version'], data, data.html);
88
+ }
89
+ export function codeTemplate(applyPrefix, data) {
90
+ const {
91
+ text,
92
+ lang
93
+ } = data;
94
+ const code = highlightCode(text, lang);
95
+ const langClass = lang ? ` class="${applyPrefix(`language-${lang}`)}"` : '';
96
+ return `<pre><code${langClass}>${code}</code></pre>`;
97
+ }
package/lib/common.d.ts CHANGED
@@ -12,4 +12,7 @@ export * from './tracing';
12
12
  export * from './merge';
13
13
  export * from './map';
14
14
  export * from './visit';
15
+ export * from './draftjs';
16
+ export * from './templates';
17
+ export * from './highlight-code';
15
18
  //# sourceMappingURL=common.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"common.d.ts","sourceRoot":"","sources":["../../src/common.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,QAAQ,CAAC;AACvB,cAAc,QAAQ,CAAC;AACvB,cAAc,SAAS,CAAC;AACxB,cAAc,WAAW,CAAC;AAC1B,cAAc,eAAe,CAAC;AAC9B,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC;AACxB,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC;AAC1B,cAAc,SAAS,CAAC;AACxB,cAAc,OAAO,CAAC;AACtB,cAAc,SAAS,CAAC"}
1
+ {"version":3,"file":"common.d.ts","sourceRoot":"","sources":["../../src/common.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,QAAQ,CAAC;AACvB,cAAc,QAAQ,CAAC;AACvB,cAAc,SAAS,CAAC;AACxB,cAAc,WAAW,CAAC;AAC1B,cAAc,eAAe,CAAC;AAC9B,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC;AACxB,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC;AAC1B,cAAc,SAAS,CAAC;AACxB,cAAc,OAAO,CAAC;AACtB,cAAc,SAAS,CAAC;AACxB,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC;AAC5B,cAAc,kBAAkB,CAAC"}
package/lib/common.js CHANGED
@@ -184,4 +184,43 @@ Object.keys(_visit).forEach(function (key) {
184
184
  return _visit[key];
185
185
  }
186
186
  });
187
+ });
188
+
189
+ var _draftjs = require("./draftjs");
190
+
191
+ Object.keys(_draftjs).forEach(function (key) {
192
+ if (key === "default" || key === "__esModule") return;
193
+ if (key in exports && exports[key] === _draftjs[key]) return;
194
+ Object.defineProperty(exports, key, {
195
+ enumerable: true,
196
+ get: function () {
197
+ return _draftjs[key];
198
+ }
199
+ });
200
+ });
201
+
202
+ var _templates = require("./templates");
203
+
204
+ Object.keys(_templates).forEach(function (key) {
205
+ if (key === "default" || key === "__esModule") return;
206
+ if (key in exports && exports[key] === _templates[key]) return;
207
+ Object.defineProperty(exports, key, {
208
+ enumerable: true,
209
+ get: function () {
210
+ return _templates[key];
211
+ }
212
+ });
213
+ });
214
+
215
+ var _highlightCode = require("./highlight-code");
216
+
217
+ Object.keys(_highlightCode).forEach(function (key) {
218
+ if (key === "default" || key === "__esModule") return;
219
+ if (key in exports && exports[key] === _highlightCode[key]) return;
220
+ Object.defineProperty(exports, key, {
221
+ enumerable: true,
222
+ get: function () {
223
+ return _highlightCode[key];
224
+ }
225
+ });
187
226
  });
@@ -0,0 +1,47 @@
1
+ import { RawDraftContentBlock } from 'draft-js';
2
+ export interface DraftjsTemplateResult {
3
+ contentBlock: RawDraftContentBlock;
4
+ entities: any;
5
+ }
6
+ export interface DraftjsLinkOpenParams {
7
+ text: string;
8
+ href: string;
9
+ depth: number;
10
+ key: number;
11
+ offset: number;
12
+ }
13
+ export interface DraftjsImageParams {
14
+ captionText: string;
15
+ creditText: string;
16
+ alignment?: string;
17
+ size?: string;
18
+ assetId: string;
19
+ linkUrl: string;
20
+ linkIsExternal: boolean;
21
+ depth: number;
22
+ key: number;
23
+ path: string;
24
+ }
25
+ export declare const getDraftjsImage: (params: DraftjsImageParams) => DraftjsTemplateResult | undefined;
26
+ export interface DraftjsPullquoteParams {
27
+ text: string;
28
+ depth: number;
29
+ }
30
+ export declare const getDraftjsPullquote: (params: DraftjsPullquoteParams) => DraftjsTemplateResult;
31
+ export interface DraftjsOembedParams {
32
+ html: string;
33
+ depth: number;
34
+ key: number;
35
+ width?: number;
36
+ height?: number;
37
+ url?: string;
38
+ author_name?: string;
39
+ author_url?: string;
40
+ type?: string;
41
+ cache_age?: number;
42
+ provider_name?: string;
43
+ provider_url?: string;
44
+ version?: string;
45
+ }
46
+ export declare const getDraftjsOembed: (params: DraftjsOembedParams) => DraftjsTemplateResult;
47
+ //# sourceMappingURL=draftjs-templates.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"draftjs-templates.d.ts","sourceRoot":"","sources":["../../src/draftjs-templates.ts"],"names":[],"mappings":"AAKA,OAAO,EAAC,oBAAoB,EAAC,MAAM,UAAU,CAAC;AAG9C,MAAM,WAAW,qBAAqB;IACpC,YAAY,EAAE,oBAAoB,CAAC;IACnC,QAAQ,EAAE,GAAG,CAAC;CACf;AAED,MAAM,WAAW,qBAAqB;IACpC,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;IACZ,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,kBAAkB;IACjC,WAAW,EAAE,MAAM,CAAC;IACpB,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;IAChB,cAAc,EAAE,OAAO,CAAC;IACxB,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;CACd;AAED,eAAO,MAAM,eAAe,WAAY,kBAAkB,KAAG,qBAAqB,GAAG,SAmEpF,CAAC;AAEF,MAAM,WAAW,sBAAsB;IACrC,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;CACf;AAED,eAAO,MAAM,mBAAmB,WAAY,sBAAsB,KAAG,qBAcpE,CAAC;AAEF,MAAM,WAAW,mBAAmB;IAClC,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,eAAO,MAAM,gBAAgB,WAAY,mBAAmB,KAAG,qBAsD9D,CAAC"}
@@ -0,0 +1,168 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.getDraftjsPullquote = exports.getDraftjsOembed = exports.getDraftjsImage = void 0;
7
+
8
+ var _shortid = _interopRequireDefault(require("shortid"));
9
+
10
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
+
12
+ /* eslint-disable camelcase */
13
+
14
+ /*
15
+ * The intention is to move all this to the client package in the end, but right now it's here
16
+ * to be shared between the draftjs / mdx implementations, which span the client / server differently.
17
+ */
18
+ const getDraftjsImage = params => {
19
+ const {
20
+ captionText,
21
+ creditText,
22
+ alignment,
23
+ size,
24
+ assetId,
25
+ linkUrl,
26
+ linkIsExternal,
27
+ depth,
28
+ key,
29
+ path
30
+ } = params;
31
+
32
+ const blockKey = _shortid.default.generate();
33
+
34
+ return {
35
+ contentBlock: {
36
+ key: _shortid.default.generate(),
37
+ text: ' ',
38
+ type: 'atomic',
39
+ depth,
40
+ inlineStyleRanges: [],
41
+ entityRanges: [{
42
+ offset: 0,
43
+ length: 1,
44
+ key
45
+ }],
46
+ data: {}
47
+ },
48
+ entities: {
49
+ [key]: {
50
+ type: 'image',
51
+ mutability: 'IMMUTABLE',
52
+ data: {
53
+ id: assetId,
54
+ path,
55
+ caption: {
56
+ blocks: [{
57
+ key: blockKey,
58
+ text: captionText,
59
+ type: 'unstyled',
60
+ depth,
61
+ inlineStyleRanges: [],
62
+ entityRanges: [],
63
+ data: {}
64
+ }],
65
+ entityMap: {}
66
+ },
67
+ credit: {
68
+ blocks: [{
69
+ key: blockKey,
70
+ text: creditText,
71
+ type: 'unstyled',
72
+ depth,
73
+ inlineStyleRanges: [],
74
+ entityRanges: [],
75
+ data: {}
76
+ }],
77
+ entityMap: {}
78
+ },
79
+ link: {
80
+ url: linkUrl,
81
+ external: linkIsExternal
82
+ },
83
+ alignment,
84
+ size,
85
+ contentTypeId: 'ASSET'
86
+ }
87
+ }
88
+ }
89
+ };
90
+ };
91
+
92
+ exports.getDraftjsImage = getDraftjsImage;
93
+
94
+ const getDraftjsPullquote = params => {
95
+ const {
96
+ text,
97
+ depth
98
+ } = params;
99
+ return {
100
+ contentBlock: {
101
+ key: _shortid.default.generate(),
102
+ text,
103
+ type: 'pullquote',
104
+ depth,
105
+ inlineStyleRanges: [],
106
+ entityRanges: [],
107
+ data: {}
108
+ },
109
+ entities: {}
110
+ };
111
+ };
112
+
113
+ exports.getDraftjsPullquote = getDraftjsPullquote;
114
+
115
+ const getDraftjsOembed = params => {
116
+ const {
117
+ key,
118
+ depth,
119
+ html,
120
+ width = 550,
121
+ height = undefined,
122
+ url = undefined,
123
+ author_name = undefined,
124
+ author_url = undefined,
125
+ type = undefined,
126
+ cache_age = undefined,
127
+ provider_name = undefined,
128
+ provider_url = undefined,
129
+ version = undefined
130
+ } = params;
131
+ return {
132
+ contentBlock: {
133
+ key: _shortid.default.generate(),
134
+ text: ' ',
135
+ type: 'atomic',
136
+ depth,
137
+ inlineStyleRanges: [],
138
+ entityRanges: [{
139
+ offset: 0,
140
+ length: 1,
141
+ key
142
+ }],
143
+ data: {}
144
+ },
145
+ // Only html is required
146
+ entities: {
147
+ [key]: {
148
+ type: 'oembed',
149
+ mutability: 'IMMUTABLE',
150
+ data: {
151
+ url,
152
+ author_name,
153
+ author_url,
154
+ html,
155
+ width,
156
+ height,
157
+ type,
158
+ cache_age,
159
+ provider_name,
160
+ provider_url,
161
+ version
162
+ }
163
+ }
164
+ }
165
+ };
166
+ };
167
+
168
+ exports.getDraftjsOembed = getDraftjsOembed;
@@ -0,0 +1,27 @@
1
+ import { RawDraftContentState, Entity } from 'draft-js';
2
+ export interface Content {
3
+ [name: string]: any;
4
+ _id: string;
5
+ }
6
+ export declare type MaybeContent = Content | undefined;
7
+ interface DraftEntityWithData extends Entity {
8
+ type?: string;
9
+ mutability?: string;
10
+ data?: Record<string, any>;
11
+ }
12
+ declare type DraftToMarkdownOptionsItems = Record<string, {
13
+ open: (entity?: DraftEntityWithData) => string;
14
+ close: (entity?: DraftEntityWithData) => string;
15
+ }>;
16
+ export declare function fromDraftjs(draftjs: RawDraftContentState, styleItems?: DraftToMarkdownOptionsItems, entityItems?: DraftToMarkdownOptionsItems): string;
17
+ export declare function draftjsToMd(draftjs: RawDraftContentState, assets: Record<string, MaybeContent>): string;
18
+ export declare function draftjsToMdx(draftjs: RawDraftContentState, assets: Record<string, MaybeContent>, prefix?: (tagName: string) => string): string;
19
+ export declare function mdToDraftjs(mdx: string): RawDraftContentState;
20
+ export declare function getImagePathFromUrl(url: string): string;
21
+ /**
22
+ * Make sure there is a place to put the cursor around block-level items such as images and oembeds
23
+ */
24
+ export declare function insertBreaksAroundBlocks(mdx: string): string;
25
+ export declare function mdxToDraftjs(mdx: string): RawDraftContentState;
26
+ export {};
27
+ //# sourceMappingURL=draftjs.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"draftjs.d.ts","sourceRoot":"","sources":["../../src/draftjs.ts"],"names":[],"mappings":"AAIA,OAAO,EAAC,oBAAoB,EAAwB,MAAM,EAAiB,MAAM,UAAU,CAAC;AAqB5F,MAAM,WAAW,OAAO;IACtB,CAAC,IAAI,EAAE,MAAM,GAAG,GAAG,CAAC;IACpB,GAAG,EAAE,MAAM,CAAC;CACb;AAED,oBAAY,YAAY,GAAG,OAAO,GAAG,SAAS,CAAC;AAE/C,UAAU,mBAAoB,SAAQ,MAAM;IAC1C,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CAC5B;AA2BD,aAAK,2BAA2B,GAAG,MAAM,CACvC,MAAM,EACN;IACE,IAAI,EAAE,CAAC,MAAM,CAAC,EAAE,mBAAmB,KAAK,MAAM,CAAC;IAC/C,KAAK,EAAE,CAAC,MAAM,CAAC,EAAE,mBAAmB,KAAK,MAAM,CAAC;CACjD,CACF,CAAC;AAiCF,wBAAgB,WAAW,CACzB,OAAO,EAAE,oBAAoB,EAC7B,UAAU,CAAC,EAAE,2BAA2B,EACxC,WAAW,CAAC,EAAE,2BAA2B,GACxC,MAAM,CAyER;AAED,wBAAgB,WAAW,CAAC,OAAO,EAAE,oBAAoB,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC,GAAG,MAAM,CA0DvG;AAmBD,wBAAgB,YAAY,CAC1B,OAAO,EAAE,oBAAoB,EAC7B,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC,EACpC,MAAM,8BAAqB,GAC1B,MAAM,CAyDR;AA4MD,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,oBAAoB,CAgD7D;AAED,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAEvD;AAoMD;;GAEG;AACH,wBAAgB,wBAAwB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAoB5D;AAED,wBAAgB,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,oBAAoB,CA0G9D"}