From cbb77628839e1e3dcacd10cd3a8fd316d03c9039 Mon Sep 17 00:00:00 2001 From: Zach Vinless Date: Tue, 11 Dec 2018 08:23:07 -0800 Subject: [PATCH] feat(editor-components): support title in image component (#1862) --- .../src/__tests__/index.spec.js | 72 +++++++++++++++++++ .../src/index.js | 14 +++- .../src/serializers/remarkImagesToText.js | 4 +- 3 files changed, 85 insertions(+), 5 deletions(-) create mode 100644 packages/netlify-cms-editor-component-image/src/__tests__/index.spec.js diff --git a/packages/netlify-cms-editor-component-image/src/__tests__/index.spec.js b/packages/netlify-cms-editor-component-image/src/__tests__/index.spec.js new file mode 100644 index 00000000..6468dfc6 --- /dev/null +++ b/packages/netlify-cms-editor-component-image/src/__tests__/index.spec.js @@ -0,0 +1,72 @@ +import component from '../index'; + +const getAsset = path => path; +const image = '/image'; +const alt = 'alt'; +const title = 'title'; + +describe('editor component image', () => { + it('should generate empty markdown image from empty object', () => { + expect(component.toBlock({})).toEqual(`![]()`); + }); + + it('should generate valid markdown from path', () => { + expect(component.toBlock({ image })).toEqual(`![](/image)`); + }); + + it('should generate valid markdown from path and alt text', () => { + expect(component.toBlock({ image, alt })).toEqual(`![alt](/image)`); + }); + + it('should generate valid markdown from path and title', () => { + expect(component.toBlock({ image, title })).toEqual(`![](/image "title")`); + }); + + it('should generate valid markdown from path, alt text, and title ', () => { + expect(component.toBlock({ image, alt, title })).toEqual(`![alt](/image "title")`); + }); + + it('should escape quotes in title', () => { + expect(component.toBlock({ image, alt, title: `"ti"tle"` })).toEqual( + `![alt](/image "\\"ti\\"tle\\"")`, + ); + }); + + it('should generate valid react props', () => { + expect(component.toPreview({ image, alt, title }, getAsset)).toMatchObject({ + props: { src: image, alt, title }, + }); + }); + + it('should match markdown with no properties defined', () => { + expect(`![]()`).toMatch(component.pattern); + }); + + it('should match markdown with path', () => { + expect(`![](/image)`).toMatch(component.pattern); + }); + + it('should match markdown with path and alt text', () => { + expect(`![alt](/image)`).toMatch(component.pattern); + }); + + it('should match markdown with path and title', () => { + expect(`![](/image "title")`).toMatch(component.pattern); + }); + + it('should match markdown with path, alt text, and title', () => { + expect(`![alt](/image "title")`).toMatch(component.pattern); + }); + + it('should match markdown with path, alt text, and title', () => { + expect(`![alt](/image "title")`).toMatch(component.pattern); + }); + + it('should match markdown with arbitrary amount of whitespace', () => { + expect(`![alt](/image "title")`).toMatch(component.pattern); + }); + + it('should match markdown with quoted title', () => { + expect(`![alt](/image "\\"ti\\"tle\\"")`).toMatch(component.pattern); + }); +}); diff --git a/packages/netlify-cms-editor-component-image/src/index.js b/packages/netlify-cms-editor-component-image/src/index.js index 9cd0544b..10dd83d8 100644 --- a/packages/netlify-cms-editor-component-image/src/index.js +++ b/packages/netlify-cms-editor-component-image/src/index.js @@ -7,11 +7,15 @@ const image = { match && { image: match[2], alt: match[1], + title: match[4], }, - toBlock: data => `![${data.alt || ''}](${data.image || ''})`, + toBlock: ({ alt, image, title }) => + `![${alt || ''}](${image || ''}${title ? ` "${title.replace(/"/g, '\\"')}"` : ''})`, // eslint-disable-next-line react/display-name - toPreview: (data, getAsset) => {data.alt, - pattern: /^!\[(.*)\]\((.*)\)$/, + toPreview: ({ alt, image, title }, getAsset) => ( + {alt + ), + pattern: /^!\[(.*)\]\((.*?)(\s"(.*)")?\)$/, fields: [ { label: 'Image', @@ -25,6 +29,10 @@ const image = { label: 'Alt Text', name: 'alt', }, + { + label: 'Title', + name: 'title', + }, ], }; diff --git a/packages/netlify-cms-widget-markdown/src/serializers/remarkImagesToText.js b/packages/netlify-cms-widget-markdown/src/serializers/remarkImagesToText.js index 6159eee3..6f305a12 100644 --- a/packages/netlify-cms-widget-markdown/src/serializers/remarkImagesToText.js +++ b/packages/netlify-cms-widget-markdown/src/serializers/remarkImagesToText.js @@ -15,8 +15,8 @@ export default function remarkImagesToText() { child.children.length === 1 && child.children[0].type === 'image' ) { - const { alt, url } = child.children[0]; - const value = `![${alt || ''}](${url || ''})`; + const { alt, url, title } = child.children[0]; + const value = `![${alt || ''}](${url || ''}${title ? ` "${title}"` : ''})`; child.children = [{ type: 'text', value }]; } return child;