@storybook/addon-docs 6.5.0-alpha.50 → 6.5.0-alpha.51

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.
@@ -75,7 +75,10 @@ var getStorySource = function getStorySource(storyId, sourceContext) {
75
75
  var sources = sourceContext.sources; // source rendering is async so source is unavailable at the start of the render cycle,
76
76
  // so we fail gracefully here without warning
77
77
 
78
- return (sources === null || sources === void 0 ? void 0 : sources[storyId]) || '';
78
+ return (sources === null || sources === void 0 ? void 0 : sources[storyId]) || {
79
+ code: '',
80
+ format: false
81
+ };
79
82
  };
80
83
 
81
84
  var getSnippet = function getSnippet(snippet, story) {
@@ -127,6 +130,8 @@ var getSourceProps = function getSourceProps(props, docsContext, sourceContext)
127
130
  var multiProps = props;
128
131
  var source = codeProps.code; // prefer user-specified code
129
132
 
133
+ var format = codeProps.format; // prefer user-specified code
134
+
130
135
  var targetIds = multiProps.ids || [singleProps.id || currentId];
131
136
  var storyIds = targetIds.map(function (targetId) {
132
137
  return targetId === _types.CURRENT_SELECTION ? currentId : targetId;
@@ -141,8 +146,15 @@ var getSourceProps = function getSourceProps(props, docsContext, sourceContext)
141
146
  }
142
147
 
143
148
  if (!source) {
149
+ // just take the format from the first story, given how they're all concatinated together...
150
+ // TODO: we should consider sending an event with all the sources separately, instead of concatenating them here
151
+ var _getStorySource = getStorySource(storyIds[0], sourceContext);
152
+
153
+ format = _getStorySource.format;
144
154
  source = storyIds.map(function (storyId, idx) {
145
- var storySource = getStorySource(storyId, sourceContext);
155
+ var _getStorySource2 = getStorySource(storyId, sourceContext),
156
+ storySource = _getStorySource2.code;
157
+
146
158
  var storyObj = stories[idx];
147
159
  return getSnippet(storySource, storyObj);
148
160
  }).join('\n\n');
@@ -158,6 +170,7 @@ var getSourceProps = function getSourceProps(props, docsContext, sourceContext)
158
170
  return source ? {
159
171
  code: source,
160
172
  state: state,
173
+ format: format,
161
174
  language: props.language || docsLanguage || 'jsx',
162
175
  dark: props.dark || false
163
176
  } : {
@@ -79,25 +79,33 @@ var SourceContainer = function SourceContainer(_ref) {
79
79
  var channel = _addons.addons.getChannel();
80
80
 
81
81
  (0, _react.useEffect)(function () {
82
- var handleSnippetRendered = function handleSnippetRendered(id, newItem) {
83
- if (newItem !== sources[id]) {
84
- setSources(function (current) {
85
- var newSources = Object.assign({}, current, _defineProperty({}, id, newItem));
82
+ var handleSnippetRendered = function handleSnippetRendered(id, newSource) {
83
+ var format = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
86
84
 
87
- if (!(0, _fastDeepEqual.default)(current, newSources)) {
88
- return newSources;
89
- }
90
-
91
- return current;
92
- });
85
+ // optimization: if the source is the same, ignore the incoming event
86
+ if (sources[id] && sources[id].code === newSource) {
87
+ return;
93
88
  }
89
+
90
+ setSources(function (current) {
91
+ var newSources = Object.assign({}, current, _defineProperty({}, id, {
92
+ code: newSource,
93
+ format: format
94
+ }));
95
+
96
+ if (!(0, _fastDeepEqual.default)(current, newSources)) {
97
+ return newSources;
98
+ }
99
+
100
+ return current;
101
+ });
94
102
  };
95
103
 
96
104
  channel.on(_shared.SNIPPET_RENDERED, handleSnippetRendered);
97
105
  return function () {
98
106
  return channel.off(_shared.SNIPPET_RENDERED, handleSnippetRendered);
99
107
  };
100
- });
108
+ }, []);
101
109
  return /*#__PURE__*/_react.default.createElement(SourceContext.Provider, {
102
110
  value: {
103
111
  sources: sources
@@ -33,7 +33,10 @@ var getStorySource = function getStorySource(storyId, sourceContext) {
33
33
  var sources = sourceContext.sources; // source rendering is async so source is unavailable at the start of the render cycle,
34
34
  // so we fail gracefully here without warning
35
35
 
36
- return (sources === null || sources === void 0 ? void 0 : sources[storyId]) || '';
36
+ return (sources === null || sources === void 0 ? void 0 : sources[storyId]) || {
37
+ code: '',
38
+ format: false
39
+ };
37
40
  };
38
41
 
39
42
  var getSnippet = function getSnippet(snippet, story) {
@@ -85,6 +88,8 @@ export var getSourceProps = function getSourceProps(props, docsContext, sourceCo
85
88
  var multiProps = props;
86
89
  var source = codeProps.code; // prefer user-specified code
87
90
 
91
+ var format = codeProps.format; // prefer user-specified code
92
+
88
93
  var targetIds = multiProps.ids || [singleProps.id || currentId];
89
94
  var storyIds = targetIds.map(function (targetId) {
90
95
  return targetId === CURRENT_SELECTION ? currentId : targetId;
@@ -99,8 +104,15 @@ export var getSourceProps = function getSourceProps(props, docsContext, sourceCo
99
104
  }
100
105
 
101
106
  if (!source) {
107
+ // just take the format from the first story, given how they're all concatinated together...
108
+ // TODO: we should consider sending an event with all the sources separately, instead of concatenating them here
109
+ var _getStorySource = getStorySource(storyIds[0], sourceContext);
110
+
111
+ format = _getStorySource.format;
102
112
  source = storyIds.map(function (storyId, idx) {
103
- var storySource = getStorySource(storyId, sourceContext);
113
+ var _getStorySource2 = getStorySource(storyId, sourceContext),
114
+ storySource = _getStorySource2.code;
115
+
104
116
  var storyObj = stories[idx];
105
117
  return getSnippet(storySource, storyObj);
106
118
  }).join('\n\n');
@@ -116,6 +128,7 @@ export var getSourceProps = function getSourceProps(props, docsContext, sourceCo
116
128
  return source ? {
117
129
  code: source,
118
130
  state: state,
131
+ format: format,
119
132
  language: props.language || docsLanguage || 'jsx',
120
133
  dark: props.dark || false
121
134
  } : {
@@ -42,25 +42,33 @@ export var SourceContainer = function SourceContainer(_ref) {
42
42
 
43
43
  var channel = addons.getChannel();
44
44
  useEffect(function () {
45
- var handleSnippetRendered = function handleSnippetRendered(id, newItem) {
46
- if (newItem !== sources[id]) {
47
- setSources(function (current) {
48
- var newSources = Object.assign({}, current, _defineProperty({}, id, newItem));
45
+ var handleSnippetRendered = function handleSnippetRendered(id, newSource) {
46
+ var format = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
49
47
 
50
- if (!deepEqual(current, newSources)) {
51
- return newSources;
52
- }
53
-
54
- return current;
55
- });
48
+ // optimization: if the source is the same, ignore the incoming event
49
+ if (sources[id] && sources[id].code === newSource) {
50
+ return;
56
51
  }
52
+
53
+ setSources(function (current) {
54
+ var newSources = Object.assign({}, current, _defineProperty({}, id, {
55
+ code: newSource,
56
+ format: format
57
+ }));
58
+
59
+ if (!deepEqual(current, newSources)) {
60
+ return newSources;
61
+ }
62
+
63
+ return current;
64
+ });
57
65
  };
58
66
 
59
67
  channel.on(SNIPPET_RENDERED, handleSnippetRendered);
60
68
  return function () {
61
69
  return channel.off(SNIPPET_RENDERED, handleSnippetRendered);
62
70
  };
63
- });
71
+ }, []);
64
72
  return /*#__PURE__*/React.createElement(SourceContext.Provider, {
65
73
  value: {
66
74
  sources: sources
@@ -31,7 +31,10 @@ const getStorySource = (storyId, sourceContext) => {
31
31
  } = sourceContext; // source rendering is async so source is unavailable at the start of the render cycle,
32
32
  // so we fail gracefully here without warning
33
33
 
34
- return (sources === null || sources === void 0 ? void 0 : sources[storyId]) || '';
34
+ return (sources === null || sources === void 0 ? void 0 : sources[storyId]) || {
35
+ code: '',
36
+ format: false
37
+ };
35
38
  };
36
39
 
37
40
  const getSnippet = (snippet, story) => {
@@ -86,6 +89,10 @@ export const getSourceProps = (props, docsContext, sourceContext) => {
86
89
  const multiProps = props;
87
90
  let source = codeProps.code; // prefer user-specified code
88
91
 
92
+ let {
93
+ format
94
+ } = codeProps; // prefer user-specified code
95
+
89
96
  const targetIds = multiProps.ids || [singleProps.id || currentId];
90
97
  const storyIds = targetIds.map(targetId => targetId === CURRENT_SELECTION ? currentId : targetId);
91
98
  const stories = useStories(storyIds, docsContext);
@@ -98,8 +105,15 @@ export const getSourceProps = (props, docsContext, sourceContext) => {
98
105
  }
99
106
 
100
107
  if (!source) {
108
+ // just take the format from the first story, given how they're all concatinated together...
109
+ // TODO: we should consider sending an event with all the sources separately, instead of concatenating them here
110
+ ({
111
+ format
112
+ } = getStorySource(storyIds[0], sourceContext));
101
113
  source = storyIds.map((storyId, idx) => {
102
- const storySource = getStorySource(storyId, sourceContext);
114
+ const {
115
+ code: storySource
116
+ } = getStorySource(storyId, sourceContext);
103
117
  const storyObj = stories[idx];
104
118
  return getSnippet(storySource, storyObj);
105
119
  }).join('\n\n');
@@ -118,6 +132,7 @@ export const getSourceProps = (props, docsContext, sourceContext) => {
118
132
  return source ? {
119
133
  code: source,
120
134
  state,
135
+ format,
121
136
  language: props.language || docsLanguage || 'jsx',
122
137
  dark: props.dark || false
123
138
  } : {
@@ -11,25 +11,31 @@ export const SourceContainer = ({
11
11
  const [sources, setSources] = useState({});
12
12
  const channel = addons.getChannel();
13
13
  useEffect(() => {
14
- const handleSnippetRendered = (id, newItem) => {
15
- if (newItem !== sources[id]) {
16
- setSources(current => {
17
- const newSources = Object.assign({}, current, {
18
- [id]: newItem
19
- });
14
+ const handleSnippetRendered = (id, newSource, format = false) => {
15
+ // optimization: if the source is the same, ignore the incoming event
16
+ if (sources[id] && sources[id].code === newSource) {
17
+ return;
18
+ }
20
19
 
21
- if (!deepEqual(current, newSources)) {
22
- return newSources;
20
+ setSources(current => {
21
+ const newSources = Object.assign({}, current, {
22
+ [id]: {
23
+ code: newSource,
24
+ format
23
25
  }
24
-
25
- return current;
26
26
  });
27
- }
27
+
28
+ if (!deepEqual(current, newSources)) {
29
+ return newSources;
30
+ }
31
+
32
+ return current;
33
+ });
28
34
  };
29
35
 
30
36
  channel.on(SNIPPET_RENDERED, handleSnippetRendered);
31
37
  return () => channel.off(SNIPPET_RENDERED, handleSnippetRendered);
32
- });
38
+ }, []);
33
39
  return /*#__PURE__*/React.createElement(SourceContext.Provider, {
34
40
  value: {
35
41
  sources
@@ -1,5 +1,5 @@
1
- import { FC } from 'react';
2
- import { SourceProps as PureSourceProps } from '@storybook/components';
1
+ import { ComponentProps, FC } from 'react';
2
+ import { Source as PureSource } from '@storybook/components';
3
3
  import { DocsContextProps } from './DocsContext';
4
4
  import { SourceContextProps } from './SourceContainer';
5
5
  export declare enum SourceState {
@@ -10,6 +10,7 @@ export declare enum SourceState {
10
10
  interface CommonProps {
11
11
  language?: string;
12
12
  dark?: boolean;
13
+ format?: PureSourceProps['format'];
13
14
  code?: string;
14
15
  }
15
16
  declare type SingleSourceProps = {
@@ -26,11 +27,12 @@ declare type SourceProps = SingleSourceProps | MultiSourceProps | CodeProps | No
26
27
  declare type SourceStateProps = {
27
28
  state: SourceState;
28
29
  };
30
+ declare type PureSourceProps = ComponentProps<typeof PureSource>;
29
31
  export declare const getSourceProps: (props: SourceProps, docsContext: DocsContextProps<any>, sourceContext: SourceContextProps) => PureSourceProps & SourceStateProps;
30
32
  /**
31
33
  * Story source doc block renders source code if provided,
32
34
  * or the source for a story if `storyId` is provided, or
33
35
  * the source for the current story if nothing is provided.
34
36
  */
35
- export declare const Source: FC<SourceProps>;
37
+ export declare const Source: FC<PureSourceProps>;
36
38
  export {};
@@ -1,6 +1,10 @@
1
1
  import { FC, Context } from 'react';
2
+ import { SyntaxHighlighterFormatTypes } from '@storybook/components';
2
3
  import { StoryId } from '@storybook/api';
3
- export declare type SourceItem = string;
4
+ export interface SourceItem {
5
+ code: string;
6
+ format: SyntaxHighlighterFormatTypes;
7
+ }
4
8
  export declare type StorySources = Record<StoryId, SourceItem>;
5
9
  export interface SourceContextProps {
6
10
  sources: StorySources;
@@ -1,5 +1,5 @@
1
- import { FC } from 'react';
2
- import { SourceProps as PureSourceProps } from '@storybook/components';
1
+ import { ComponentProps, FC } from 'react';
2
+ import { Source as PureSource } from '@storybook/components';
3
3
  import { DocsContextProps } from './DocsContext';
4
4
  import { SourceContextProps } from './SourceContainer';
5
5
  export declare enum SourceState {
@@ -10,6 +10,7 @@ export declare enum SourceState {
10
10
  interface CommonProps {
11
11
  language?: string;
12
12
  dark?: boolean;
13
+ format?: PureSourceProps['format'];
13
14
  code?: string;
14
15
  }
15
16
  declare type SingleSourceProps = {
@@ -26,11 +27,12 @@ declare type SourceProps = SingleSourceProps | MultiSourceProps | CodeProps | No
26
27
  declare type SourceStateProps = {
27
28
  state: SourceState;
28
29
  };
30
+ declare type PureSourceProps = ComponentProps<typeof PureSource>;
29
31
  export declare const getSourceProps: (props: SourceProps, docsContext: DocsContextProps<any>, sourceContext: SourceContextProps) => PureSourceProps & SourceStateProps;
30
32
  /**
31
33
  * Story source doc block renders source code if provided,
32
34
  * or the source for a story if `storyId` is provided, or
33
35
  * the source for the current story if nothing is provided.
34
36
  */
35
- export declare const Source: FC<SourceProps>;
37
+ export declare const Source: FC<PureSourceProps>;
36
38
  export {};
@@ -1,6 +1,10 @@
1
1
  import { FC, Context } from 'react';
2
- import { StoryId } from '@storybook/api';
3
- export declare type SourceItem = string;
2
+ import type { SyntaxHighlighterFormatTypes } from '@storybook/components';
3
+ import type { StoryId } from '@storybook/api';
4
+ export interface SourceItem {
5
+ code: string;
6
+ format: SyntaxHighlighterFormatTypes;
7
+ }
4
8
  export declare type StorySources = Record<StoryId, SourceItem>;
5
9
  export interface SourceContextProps {
6
10
  sources: StorySources;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@storybook/addon-docs",
3
- "version": "6.5.0-alpha.50",
3
+ "version": "6.5.0-alpha.51",
4
4
  "description": "Document component usage and properties in Markdown",
5
5
  "keywords": [
6
6
  "addon",
@@ -59,20 +59,20 @@
59
59
  "@babel/preset-env": "^7.12.11",
60
60
  "@jest/transform": "^26.6.2",
61
61
  "@mdx-js/react": "^1.6.22",
62
- "@storybook/addons": "6.5.0-alpha.50",
63
- "@storybook/api": "6.5.0-alpha.50",
64
- "@storybook/components": "6.5.0-alpha.50",
65
- "@storybook/core-common": "6.5.0-alpha.50",
66
- "@storybook/core-events": "6.5.0-alpha.50",
62
+ "@storybook/addons": "6.5.0-alpha.51",
63
+ "@storybook/api": "6.5.0-alpha.51",
64
+ "@storybook/components": "6.5.0-alpha.51",
65
+ "@storybook/core-common": "6.5.0-alpha.51",
66
+ "@storybook/core-events": "6.5.0-alpha.51",
67
67
  "@storybook/csf": "0.0.2--canary.507502b.0",
68
- "@storybook/docs-tools": "6.5.0-alpha.50",
68
+ "@storybook/docs-tools": "6.5.0-alpha.51",
69
69
  "@storybook/mdx1-csf": "canary",
70
- "@storybook/node-logger": "6.5.0-alpha.50",
71
- "@storybook/postinstall": "6.5.0-alpha.50",
72
- "@storybook/preview-web": "6.5.0-alpha.50",
73
- "@storybook/source-loader": "6.5.0-alpha.50",
74
- "@storybook/store": "6.5.0-alpha.50",
75
- "@storybook/theming": "6.5.0-alpha.50",
70
+ "@storybook/node-logger": "6.5.0-alpha.51",
71
+ "@storybook/postinstall": "6.5.0-alpha.51",
72
+ "@storybook/preview-web": "6.5.0-alpha.51",
73
+ "@storybook/source-loader": "6.5.0-alpha.51",
74
+ "@storybook/store": "6.5.0-alpha.51",
75
+ "@storybook/theming": "6.5.0-alpha.51",
76
76
  "core-js": "^3.8.2",
77
77
  "fast-deep-equal": "^3.1.3",
78
78
  "global": "^4.4.0",
@@ -118,7 +118,7 @@
118
118
  "publishConfig": {
119
119
  "access": "public"
120
120
  },
121
- "gitHead": "6cf4571e5a1200613de94aa066fe93f75aec6ad1",
121
+ "gitHead": "cfcdc7fb74d3cf60eae8dd0b5a626d67ed180d42",
122
122
  "sbmodern": "dist/modern/index.js",
123
123
  "storybook": {
124
124
  "displayName": "Docs",