UNPKG

4.39 kBJavaScriptView Raw
1"use strict";
2
3require("core-js/modules/es.symbol.js");
4
5require("core-js/modules/es.symbol.description.js");
6
7require("core-js/modules/es.object.to-string.js");
8
9require("core-js/modules/es.symbol.iterator.js");
10
11require("core-js/modules/es.string.iterator.js");
12
13require("core-js/modules/es.array.iterator.js");
14
15require("core-js/modules/web.dom-collections.iterator.js");
16
17require("core-js/modules/es.array.from.js");
18
19require("core-js/modules/es.function.name.js");
20
21Object.defineProperty(exports, "__esModule", {
22 value: true
23});
24exports.enhanceSource = void 0;
25
26require("core-js/modules/es.array.join.js");
27
28require("core-js/modules/es.array.concat.js");
29
30require("core-js/modules/es.array.slice.js");
31
32require("core-js/modules/es.string.replace.js");
33
34require("core-js/modules/es.regexp.exec.js");
35
36require("core-js/modules/es.string.split.js");
37
38var _store = require("@storybook/store");
39
40function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
41
42function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
43
44function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
45
46function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }
47
48function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
49
50function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
51
52/**
53 * given a location, extract the text from the full source
54 */
55function extractSource(location, lines) {
56 var start = location.startBody,
57 end = location.endBody;
58
59 if (start.line === end.line && lines[start.line - 1] !== undefined) {
60 return lines[start.line - 1].substring(start.col, end.col);
61 } // NOTE: storysource locations are 1-based not 0-based!
62
63
64 var startLine = lines[start.line - 1];
65 var endLine = lines[end.line - 1];
66
67 if (startLine === undefined || endLine === undefined) {
68 return null;
69 }
70
71 return [startLine.substring(start.col)].concat(_toConsumableArray(lines.slice(start.line, end.line - 1)), [endLine.substring(0, end.col)]).join('\n');
72} // ============================================================
73// END @storybook/source-loader/extract-source
74// ============================================================
75
76
77/**
78 * Replaces full story id name like: story-kind--story-name -> story-name
79 * @param id
80 */
81var storyIdToSanitizedStoryName = function storyIdToSanitizedStoryName(id) {
82 return id.replace(/^.*?--/, '');
83};
84
85var extract = function extract(targetId, _ref) {
86 var source = _ref.source,
87 locationsMap = _ref.locationsMap;
88
89 if (!locationsMap) {
90 return source;
91 }
92
93 var sanitizedStoryName = storyIdToSanitizedStoryName(targetId);
94 var location = locationsMap[sanitizedStoryName];
95
96 if (!location) {
97 return source;
98 }
99
100 var lines = source.split('\n');
101 return extractSource(location, lines);
102};
103
104var enhanceSource = function enhanceSource(story) {
105 var _docs$source;
106
107 var id = story.id,
108 parameters = story.parameters;
109 var storySource = parameters.storySource,
110 _parameters$docs = parameters.docs,
111 docs = _parameters$docs === void 0 ? {} : _parameters$docs;
112 var transformSource = docs.transformSource; // no input or user has manually overridden the output
113
114 if (!(storySource !== null && storySource !== void 0 && storySource.source) || (_docs$source = docs.source) !== null && _docs$source !== void 0 && _docs$source.code) {
115 return null;
116 }
117
118 var input = extract(id, storySource);
119 var code = transformSource ? transformSource(input, story) : input;
120 return {
121 docs: (0, _store.combineParameters)(docs, {
122 source: {
123 code: code
124 }
125 })
126 };
127};
128
129exports.enhanceSource = enhanceSource;
\No newline at end of file