Commit e28237cf authored by Himanshu Kapoor's avatar Himanshu Kapoor

Rename parseFilename and add comments

parent 86ee3478
...@@ -9,7 +9,7 @@ import { ...@@ -9,7 +9,7 @@ import {
GlTooltipDirective as GlTooltip, GlTooltipDirective as GlTooltip,
} from '@gitlab/ui'; } from '@gitlab/ui';
import { acceptedMimes } from '../services/upload_helpers'; import { acceptedMimes } from '../services/upload_helpers';
import { parseFilename } from '../services/utils'; import { extractFilename } from '../services/utils';
export default { export default {
components: { components: {
...@@ -41,7 +41,7 @@ export default { ...@@ -41,7 +41,7 @@ export default {
.setImage({ .setImage({
src: this.imgSrc, src: this.imgSrc,
canonicalSrc: this.imgSrc, canonicalSrc: this.imgSrc,
alt: parseFilename(this.imgSrc), alt: extractFilename(this.imgSrc),
}) })
.run(); .run();
......
import axios from '~/lib/utils/axios_utils'; import axios from '~/lib/utils/axios_utils';
import { __ } from '~/locale'; import { __ } from '~/locale';
import { parseFilename, readFileAsDataURL } from './utils'; import { extractFilename, readFileAsDataURL } from './utils';
export const acceptedMimes = { export const acceptedMimes = {
image: ['image/jpeg', 'image/png', 'image/gif', 'image/jpg'], image: ['image/jpeg', 'image/png', 'image/gif', 'image/jpg'],
...@@ -66,7 +66,7 @@ const uploadImage = async ({ editor, file, uploadsPath, renderMarkdown }) => { ...@@ -66,7 +66,7 @@ const uploadImage = async ({ editor, file, uploadsPath, renderMarkdown }) => {
tr.setNodeMarkup(position, undefined, { tr.setNodeMarkup(position, undefined, {
uploading: false, uploading: false,
src: encodedSrc, src: encodedSrc,
alt: parseFilename(src), alt: extractFilename(src),
canonicalSrc, canonicalSrc,
}), }),
); );
...@@ -81,7 +81,7 @@ const uploadAttachment = async ({ editor, file, uploadsPath, renderMarkdown }) = ...@@ -81,7 +81,7 @@ const uploadAttachment = async ({ editor, file, uploadsPath, renderMarkdown }) =
const { view } = editor; const { view } = editor;
const text = parseFilename(file.name); const text = extractFilename(file.name);
const { state } = view; const { state } = view;
const { from } = state.selection; const { from } = state.selection;
......
...@@ -4,8 +4,18 @@ export const hasSelection = (tiptapEditor) => { ...@@ -4,8 +4,18 @@ export const hasSelection = (tiptapEditor) => {
return from < to; return from < to;
}; };
export const parseFilename = (src) => { /**
return src.replace(/^.*\/|\..+?$/g, '').replace(/\W+/g, ' '); * Extracts filename from a URL
*
* @example
* > extractFilename('https://gitlab.com/images/logo-full.png')
* < 'logo-full'
*
* @param {string} src The URL to extract filename from
* @returns {string}
*/
export const extractFilename = (src) => {
return src.replace(/^.*\/|\..+?$/g, '');
}; };
export const readFileAsDataURL = (file) => { export const readFileAsDataURL = (file) => {
......
...@@ -112,7 +112,7 @@ describe('content_editor/extensions/image', () => { ...@@ -112,7 +112,7 @@ describe('content_editor/extensions/image', () => {
image({ image({
canonicalSrc: 'test-file.png', canonicalSrc: 'test-file.png',
src: base64EncodedFile, src: base64EncodedFile,
alt: 'test file', alt: 'test-file',
uploading: false, uploading: false,
}), }),
), ),
...@@ -162,7 +162,7 @@ describe('content_editor/extensions/image', () => { ...@@ -162,7 +162,7 @@ describe('content_editor/extensions/image', () => {
describe('when uploading succeeds', () => { describe('when uploading succeeds', () => {
const successResponse = { const successResponse = {
link: { link: {
markdown: '[test file](test-file.zip)', markdown: '[test-file](test-file.zip)',
}, },
}; };
...@@ -171,7 +171,7 @@ describe('content_editor/extensions/image', () => { ...@@ -171,7 +171,7 @@ describe('content_editor/extensions/image', () => {
}); });
it('inserts a loading mark', (done) => { it('inserts a loading mark', (done) => {
const expectedDoc = doc(p(loading({ label: 'test file' }))); const expectedDoc = doc(p(loading({ label: 'test-file' })));
tiptapEditor.on( tiptapEditor.on(
'update', 'update',
...@@ -193,7 +193,7 @@ describe('content_editor/extensions/image', () => { ...@@ -193,7 +193,7 @@ describe('content_editor/extensions/image', () => {
canonicalSrc: 'test-file.zip', canonicalSrc: 'test-file.zip',
href: `/${group}/${project}/-/wikis/test-file.zip`, href: `/${group}/${project}/-/wikis/test-file.zip`,
}, },
'test file', 'test-file',
), ),
), ),
); );
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment