@measured/puck-plugin-heading-analyzer 0.1.4 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.css ADDED
@@ -0,0 +1,101 @@
1
+ /* css-module:/home/runner/work/puck/puck/packages/core/components/SidebarSection/styles.module.css/#css-module-data */
2
+ ._SidebarSection_jsf8x_1:last-of-type ._SidebarSection-content_jsf8x_1 {
3
+ border-bottom: none;
4
+ height: 100%;
5
+ }
6
+ ._SidebarSection-title_jsf8x_6 {
7
+ background: white;
8
+ padding: 16px;
9
+ border-bottom: 1px solid var(--puck-color-grey-8);
10
+ }
11
+ ._SidebarSection-title_jsf8x_6:hover {
12
+ opacity: 0.6;
13
+ cursor: pointer;
14
+ }
15
+ ._SidebarSection-content_jsf8x_1 {
16
+ border-bottom: 1px solid var(--puck-color-grey-8);
17
+ padding: 16px;
18
+ }
19
+ ._SidebarSection_jsf8x_1 > summary {
20
+ list-style: none;
21
+ }
22
+ ._SidebarSection_jsf8x_1 > summary::-webkit-details-marker {
23
+ display: none;
24
+ }
25
+
26
+ /* css-module:/home/runner/work/puck/puck/packages/core/components/Heading/styles.module.css/#css-module-data */
27
+ ._Heading_1y35v_1 {
28
+ display: block;
29
+ font-family: var(--puck-font-stack);
30
+ font-weight: 700;
31
+ margin: 0;
32
+ }
33
+ ._Heading_1y35v_1 b {
34
+ font-weight: 700;
35
+ }
36
+ ._Heading--xxxxl_1y35v_12 {
37
+ font-size: var(--puck-font-size-xxxxl);
38
+ letter-spacing: 0.08ch;
39
+ font-weight: 800;
40
+ }
41
+ ._Heading--xxxl_1y35v_18 {
42
+ font-size: var(--puck-font-size-xxxl);
43
+ }
44
+ ._Heading--xxl_1y35v_22 {
45
+ font-size: var(--puck-font-size-xxl);
46
+ }
47
+ ._Heading--xl_1y35v_26 {
48
+ font-size: var(--puck-font-size-xl);
49
+ }
50
+ ._Heading--l_1y35v_30 {
51
+ font-size: var(--puck-font-size-l);
52
+ }
53
+ ._Heading--m_1y35v_34 {
54
+ font-size: var(--puck-font-size-m);
55
+ }
56
+ ._Heading--s_1y35v_38 {
57
+ font-size: var(--puck-font-size-s);
58
+ }
59
+ ._Heading--xs_1y35v_42 {
60
+ font-size: var(--puck-font-size-xs);
61
+ }
62
+
63
+ /* css-module:/home/runner/work/puck/puck/packages/core/components/OutlineList/styles.module.css/#css-module-data */
64
+ ._OutlineList_gor6f_1 {
65
+ color: var(--puck-color-grey-2);
66
+ font-family: var(--puck-font-stack);
67
+ margin: 0;
68
+ margin-left: 16px;
69
+ padding-left: 16px;
70
+ position: relative;
71
+ list-style: none;
72
+ }
73
+ ._OutlineList_gor6f_1::before {
74
+ background: var(--puck-color-grey-7);
75
+ position: absolute;
76
+ left: -1px;
77
+ top: 0px;
78
+ width: 1px;
79
+ height: calc(100% - 9px);
80
+ content: "";
81
+ }
82
+ ._OutlineList_gor6f_1 li {
83
+ position: relative;
84
+ margin-bottom: 4px;
85
+ }
86
+ ._OutlineList_gor6f_1 li::before {
87
+ background: var(--puck-color-grey-7);
88
+ position: absolute;
89
+ left: -17px;
90
+ top: 9px;
91
+ width: 13px;
92
+ height: 1px;
93
+ content: "";
94
+ }
95
+ ._OutlineList_gor6f_1 ._OutlineList-clickableItem_gor6f_36:hover {
96
+ color: var(--puck-color-blue);
97
+ cursor: pointer;
98
+ }
99
+ ._OutlineList_gor6f_1 li > ._OutlineList_gor6f_1 {
100
+ margin: 8px;
101
+ }
@@ -0,0 +1,5 @@
1
+ import { Plugin } from '@measured/puck/types/Plugin';
2
+
3
+ declare const HeadingAnalyzer: Plugin;
4
+
5
+ export { HeadingAnalyzer as default };
package/dist/index.js ADDED
@@ -0,0 +1,377 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
7
+ var __getProtoOf = Object.getPrototypeOf;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
10
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
11
+ var __spreadValues = (a, b) => {
12
+ for (var prop in b || (b = {}))
13
+ if (__hasOwnProp.call(b, prop))
14
+ __defNormalProp(a, prop, b[prop]);
15
+ if (__getOwnPropSymbols)
16
+ for (var prop of __getOwnPropSymbols(b)) {
17
+ if (__propIsEnum.call(b, prop))
18
+ __defNormalProp(a, prop, b[prop]);
19
+ }
20
+ return a;
21
+ };
22
+ var __esm = (fn, res) => function __init() {
23
+ return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
24
+ };
25
+ var __commonJS = (cb, mod) => function __require() {
26
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
27
+ };
28
+ var __export = (target, all) => {
29
+ for (var name in all)
30
+ __defProp(target, name, { get: all[name], enumerable: true });
31
+ };
32
+ var __copyProps = (to, from, except, desc) => {
33
+ if (from && typeof from === "object" || typeof from === "function") {
34
+ for (let key of __getOwnPropNames(from))
35
+ if (!__hasOwnProp.call(to, key) && key !== except)
36
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
37
+ }
38
+ return to;
39
+ };
40
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
41
+ // If the importer is in node compatibility mode or this is not an ESM
42
+ // file that has been converted to a CommonJS file using a Babel-
43
+ // compatible transform (i.e. "__esModule" has not been set), then set
44
+ // "default" to the CommonJS "module.exports" for node compatibility.
45
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
46
+ mod
47
+ ));
48
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
49
+
50
+ // ../tsup-config/react-import.js
51
+ var import_react;
52
+ var init_react_import = __esm({
53
+ "../tsup-config/react-import.js"() {
54
+ "use strict";
55
+ import_react = __toESM(require("react"));
56
+ }
57
+ });
58
+
59
+ // ../../node_modules/classnames/index.js
60
+ var require_classnames = __commonJS({
61
+ "../../node_modules/classnames/index.js"(exports, module2) {
62
+ init_react_import();
63
+ (function() {
64
+ "use strict";
65
+ var hasOwn = {}.hasOwnProperty;
66
+ var nativeCodeString = "[native code]";
67
+ function classNames() {
68
+ var classes = [];
69
+ for (var i = 0; i < arguments.length; i++) {
70
+ var arg = arguments[i];
71
+ if (!arg)
72
+ continue;
73
+ var argType = typeof arg;
74
+ if (argType === "string" || argType === "number") {
75
+ classes.push(arg);
76
+ } else if (Array.isArray(arg)) {
77
+ if (arg.length) {
78
+ var inner = classNames.apply(null, arg);
79
+ if (inner) {
80
+ classes.push(inner);
81
+ }
82
+ }
83
+ } else if (argType === "object") {
84
+ if (arg.toString !== Object.prototype.toString && !arg.toString.toString().includes("[native code]")) {
85
+ classes.push(arg.toString());
86
+ continue;
87
+ }
88
+ for (var key in arg) {
89
+ if (hasOwn.call(arg, key) && arg[key]) {
90
+ classes.push(key);
91
+ }
92
+ }
93
+ }
94
+ }
95
+ return classes.join(" ");
96
+ }
97
+ if (typeof module2 !== "undefined" && module2.exports) {
98
+ classNames.default = classNames;
99
+ module2.exports = classNames;
100
+ } else if (typeof define === "function" && typeof define.amd === "object" && define.amd) {
101
+ define("classnames", [], function() {
102
+ return classNames;
103
+ });
104
+ } else {
105
+ window.classNames = classNames;
106
+ }
107
+ })();
108
+ }
109
+ });
110
+
111
+ // index.ts
112
+ var plugin_heading_analyzer_exports = {};
113
+ __export(plugin_heading_analyzer_exports, {
114
+ default: () => HeadingAnalyzer_default
115
+ });
116
+ module.exports = __toCommonJS(plugin_heading_analyzer_exports);
117
+ init_react_import();
118
+
119
+ // src/HeadingAnalyzer.tsx
120
+ init_react_import();
121
+ var import_react2 = require("react");
122
+
123
+ // ../core/components/SidebarSection/index.tsx
124
+ init_react_import();
125
+
126
+ // css-module:/home/runner/work/puck/puck/packages/core/components/SidebarSection/styles.module.css#css-module
127
+ init_react_import();
128
+ var styles_module_default = { "SidebarSection": "_SidebarSection_jsf8x_1", "SidebarSection-content": "_SidebarSection-content_jsf8x_1", "SidebarSection-title": "_SidebarSection-title_jsf8x_6" };
129
+
130
+ // ../core/lib/get-class-name-factory.ts
131
+ init_react_import();
132
+ var import_classnames = __toESM(require_classnames());
133
+ var getClassNameFactory = (rootClass, styles, { baseClass = "" } = {}) => (options = {}) => {
134
+ let descendant = false;
135
+ let modifiers = false;
136
+ if (typeof options === "string") {
137
+ descendant = options;
138
+ } else if (typeof options === "object") {
139
+ modifiers = options;
140
+ }
141
+ if (descendant) {
142
+ return baseClass + styles[`${rootClass}-${descendant}`] || "";
143
+ } else if (modifiers) {
144
+ const prefixedModifiers = {};
145
+ for (let modifier in modifiers) {
146
+ prefixedModifiers[styles[`${rootClass}--${modifier}`]] = modifiers[modifier];
147
+ }
148
+ const c = styles[rootClass];
149
+ return baseClass + (0, import_classnames.default)(__spreadValues({
150
+ [c]: !!c
151
+ }, prefixedModifiers));
152
+ } else {
153
+ return baseClass + styles[rootClass] || "";
154
+ }
155
+ };
156
+ var get_class_name_factory_default = getClassNameFactory;
157
+
158
+ // ../core/components/Heading/index.tsx
159
+ init_react_import();
160
+
161
+ // css-module:/home/runner/work/puck/puck/packages/core/components/Heading/styles.module.css#css-module
162
+ init_react_import();
163
+ var styles_module_default2 = { "Heading": "_Heading_1y35v_1", "Heading--xxxxl": "_Heading--xxxxl_1y35v_12", "Heading--xxxl": "_Heading--xxxl_1y35v_18", "Heading--xxl": "_Heading--xxl_1y35v_22", "Heading--xl": "_Heading--xl_1y35v_26", "Heading--l": "_Heading--l_1y35v_30", "Heading--m": "_Heading--m_1y35v_34", "Heading--s": "_Heading--s_1y35v_38", "Heading--xs": "_Heading--xs_1y35v_42" };
164
+
165
+ // ../core/components/Heading/index.tsx
166
+ var import_jsx_runtime = require("react/jsx-runtime");
167
+ var getClassName = get_class_name_factory_default("Heading", styles_module_default2);
168
+ var Heading = ({ children, rank, size = "m" }) => {
169
+ const Tag = rank ? `h${rank}` : "span";
170
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
171
+ Tag,
172
+ {
173
+ className: getClassName({
174
+ [size]: true
175
+ }),
176
+ children
177
+ }
178
+ );
179
+ };
180
+
181
+ // ../core/components/SidebarSection/index.tsx
182
+ var getClassName2 = get_class_name_factory_default("SidebarSection", styles_module_default);
183
+ var SidebarSection = ({
184
+ children,
185
+ title
186
+ }) => {
187
+ return /* @__PURE__ */ import_react.default.createElement("details", { className: getClassName2({}), open: true }, /* @__PURE__ */ import_react.default.createElement("summary", { className: getClassName2("title") }, /* @__PURE__ */ import_react.default.createElement(Heading, { rank: 2, size: "xs" }, title)), /* @__PURE__ */ import_react.default.createElement("div", { className: getClassName2("content") }, children));
188
+ };
189
+
190
+ // ../core/components/OutlineList/index.tsx
191
+ init_react_import();
192
+
193
+ // css-module:/home/runner/work/puck/puck/packages/core/components/OutlineList/styles.module.css#css-module
194
+ init_react_import();
195
+ var styles_module_default3 = { "OutlineList": "_OutlineList_gor6f_1", "OutlineList-clickableItem": "_OutlineList-clickableItem_gor6f_36" };
196
+
197
+ // ../core/components/OutlineList/index.tsx
198
+ var getClassName3 = get_class_name_factory_default("OutlineList", styles_module_default3);
199
+ var OutlineList = ({ children }) => {
200
+ return /* @__PURE__ */ import_react.default.createElement("ul", { className: getClassName3() }, children);
201
+ };
202
+ OutlineList.Clickable = ({ children }) => /* @__PURE__ */ import_react.default.createElement("div", { className: getClassName3("clickableItem") }, children);
203
+ OutlineList.Item = ({
204
+ children,
205
+ onClick
206
+ }) => {
207
+ return /* @__PURE__ */ import_react.default.createElement(
208
+ "li",
209
+ {
210
+ className: onClick ? getClassName3("clickableItem") : "",
211
+ onClick
212
+ },
213
+ children
214
+ );
215
+ };
216
+
217
+ // ../core/lib/scroll-into-view.ts
218
+ init_react_import();
219
+ var scrollIntoView = (el) => {
220
+ const oldStyle = __spreadValues({}, el.style);
221
+ el.style.scrollMargin = "256px";
222
+ if (el) {
223
+ el == null ? void 0 : el.scrollIntoView({ behavior: "smooth" });
224
+ el.style.scrollMargin = oldStyle.scrollMargin || "";
225
+ }
226
+ };
227
+
228
+ // src/HeadingAnalyzer.tsx
229
+ var import_react_from_json = __toESM(require("react-from-json"));
230
+ var import_jsx_runtime2 = require("react/jsx-runtime");
231
+ var dataAttr = "data-puck-heading-analyzer-id";
232
+ var getOutline = ({
233
+ addDataAttr = false
234
+ } = {}) => {
235
+ const headings = window.document.querySelector(".puck-root").querySelectorAll("h1,h2,h3,h4,h5,h6");
236
+ const _outline = [];
237
+ headings.forEach((item, i) => {
238
+ if (addDataAttr) {
239
+ item.setAttribute(dataAttr, i.toString());
240
+ }
241
+ _outline.push({
242
+ rank: parseInt(item.tagName.split("H")[1]),
243
+ text: item.textContent,
244
+ analyzeId: i.toString()
245
+ });
246
+ });
247
+ return _outline;
248
+ };
249
+ function buildHierarchy() {
250
+ var _a, _b;
251
+ const headings = getOutline({ addDataAttr: true });
252
+ const root = { rank: 0, children: [], text: "" };
253
+ let path = [root];
254
+ for (let heading of headings) {
255
+ const node = {
256
+ rank: heading.rank,
257
+ text: heading.text,
258
+ analyzeId: heading.analyzeId,
259
+ children: []
260
+ };
261
+ if (node.rank === 1) {
262
+ path = [root];
263
+ } else {
264
+ while (path[path.length - 1].rank >= node.rank) {
265
+ path.pop();
266
+ }
267
+ while (path.length < node.rank) {
268
+ const missingNode = {
269
+ rank: path.length,
270
+ missing: true,
271
+ children: [],
272
+ text: ""
273
+ };
274
+ (_a = path[path.length - 1].children) == null ? void 0 : _a.push(missingNode);
275
+ path.push(missingNode);
276
+ }
277
+ }
278
+ (_b = path[path.length - 1].children) == null ? void 0 : _b.push(node);
279
+ path.push(node);
280
+ }
281
+ return root.children;
282
+ }
283
+ var HeadingOutlineAnalyzer = ({
284
+ children,
285
+ data
286
+ }) => {
287
+ const [hierarchy, setHierarchy] = (0, import_react2.useState)([]);
288
+ const [firstRender, setFirstRender] = (0, import_react2.useState)(true);
289
+ (0, import_react2.useEffect)(() => {
290
+ if (firstRender) {
291
+ setTimeout(() => {
292
+ setHierarchy(buildHierarchy());
293
+ setFirstRender(false);
294
+ }, 100);
295
+ } else {
296
+ setHierarchy(buildHierarchy());
297
+ }
298
+ }, [data.content]);
299
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { children: [
300
+ children,
301
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(SidebarSection, { title: "Heading Outline", children: [
302
+ hierarchy.length === 0 && /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { children: "No headings." }),
303
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(OutlineList, { children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
304
+ import_react_from_json.default,
305
+ {
306
+ mapping: {
307
+ Root: (props) => /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_jsx_runtime2.Fragment, { children: props.children }),
308
+ OutlineListItem: (props) => /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(OutlineList.Item, { children: [
309
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(OutlineList.Clickable, { children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
310
+ "small",
311
+ {
312
+ onClick: typeof props.analyzeId == "undefined" ? void 0 : (e) => {
313
+ e.stopPropagation();
314
+ const el = document.querySelector(
315
+ `[${dataAttr}="${props.analyzeId}"]`
316
+ );
317
+ const oldStyle = __spreadValues({}, el.style);
318
+ if (el) {
319
+ scrollIntoView(el);
320
+ el.style.outline = "4px solid var(--puck-color-rose-5)";
321
+ el.style.outlineOffset = "4px";
322
+ setTimeout(() => {
323
+ el.style.outline = oldStyle.outline || "";
324
+ el.style.outlineOffset = oldStyle.outlineOffset || "";
325
+ }, 2e3);
326
+ }
327
+ },
328
+ children: props.missing ? /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("span", { style: { color: "var(--puck-color-red)" }, children: [
329
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("b", { children: [
330
+ "H",
331
+ props.rank
332
+ ] }),
333
+ ": Missing"
334
+ ] }) : /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("span", { children: [
335
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("b", { children: [
336
+ "H",
337
+ props.rank
338
+ ] }),
339
+ ": ",
340
+ props.text
341
+ ] })
342
+ }
343
+ ) }),
344
+ /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(OutlineList, { children: props.children })
345
+ ] })
346
+ },
347
+ entry: {
348
+ props: { children: hierarchy },
349
+ type: "Root"
350
+ },
351
+ mapProp: (prop) => {
352
+ if (prop && prop.rank) {
353
+ return {
354
+ type: "OutlineListItem",
355
+ props: prop
356
+ };
357
+ }
358
+ return prop;
359
+ }
360
+ }
361
+ ) })
362
+ ] })
363
+ ] });
364
+ };
365
+ var HeadingAnalyzer = {
366
+ renderPageFields: HeadingOutlineAnalyzer
367
+ };
368
+ var HeadingAnalyzer_default = HeadingAnalyzer;
369
+ /*! Bundled license information:
370
+
371
+ classnames/index.js:
372
+ (*!
373
+ Copyright (c) 2018 Jed Watson.
374
+ Licensed under the MIT License (MIT), see
375
+ http://jedwatson.github.io/classnames
376
+ *)
377
+ */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@measured/puck-plugin-heading-analyzer",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "private": false,
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -8,13 +8,13 @@
8
8
  "scripts": {
9
9
  "lint": "eslint \"**/*.ts*\"",
10
10
  "build": "rm -rf dist && tsup index.ts",
11
- "prepublish": "yarn build"
11
+ "prepare": "yarn build"
12
12
  },
13
13
  "files": [
14
14
  "dist"
15
15
  ],
16
16
  "devDependencies": {
17
- "@measured/puck": "^0.1.4",
17
+ "@measured/puck": "^0.1.6",
18
18
  "@types/react": "^18.2.0",
19
19
  "@types/react-dom": "^18.2.0",
20
20
  "eslint": "^7.32.0",