Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support image loading attribute #165

Merged
merged 1 commit into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/extensions/markdown/Image/ImageSpecs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const ImageAttr = {
Src: 'src',
Alt: 'alt',
Title: 'title',
Loading: 'loading',
} as const;

export const ImageSpecs: ExtensionAuto = (builder) => {
Expand All @@ -18,6 +19,7 @@ export const ImageSpecs: ExtensionAuto = (builder) => {
[ImageAttr.Src]: {},
[ImageAttr.Alt]: {default: null},
[ImageAttr.Title]: {default: null},
[ImageAttr.Loading]: {default: null},
},
group: 'inline',
draggable: true,
Expand All @@ -29,6 +31,7 @@ export const ImageSpecs: ExtensionAuto = (builder) => {
[ImageAttr.Src]: (dom as Element).getAttribute(ImageAttr.Src),
[ImageAttr.Alt]: (dom as Element).getAttribute(ImageAttr.Alt),
[ImageAttr.Title]: (dom as Element).getAttribute(ImageAttr.Title),
[ImageAttr.Loading]: (dom as Element).getAttribute(ImageAttr.Loading),
};
},
},
Expand All @@ -44,6 +47,7 @@ export const ImageSpecs: ExtensionAuto = (builder) => {
getAttrs: (tok) => ({
[ImageAttr.Src]: tok.attrGet('src'),
[ImageAttr.Title]: tok.attrGet('title') || null,
[ImageAttr.Loading]: tok.attrGet(ImageAttr.Loading) || null,
[ImageAttr.Alt]: tok.children?.[0]?.content || null,
}),
},
Expand Down
10 changes: 10 additions & 0 deletions src/extensions/yfm/ImgSize/ImgSizeSpecs/const.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {ImsizeAttr} from '@diplodoc/transform/lib/plugins/imsize/const';

export const ImgSizeAttr = {
Alt: ImsizeAttr.Alt,
Src: ImsizeAttr.Src,
Title: ImsizeAttr.Title,
Width: ImsizeAttr.Width,
Height: ImsizeAttr.Height,
Loading: 'loading',
} as const;
8 changes: 7 additions & 1 deletion src/extensions/yfm/ImgSize/ImgSizeSpecs/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import isNumber from 'is-number';
import type {NodeSpec} from 'prosemirror-model';
import imsize from '@diplodoc/transform/lib/plugins/imsize';
import {ImsizeAttr as ImgSizeAttr} from '@diplodoc/transform/lib/plugins/imsize/const';
import log from '@diplodoc/transform/lib/log';

import type {ExtensionAuto} from '../../../../core';
import {imageNodeName} from '../../../markdown/Image/const';
import {ImgSizeAttr} from './const';

type ImsizeTypedAttributes = {
[ImgSizeAttr.Src]: string;
[ImgSizeAttr.Title]: string | null;
[ImgSizeAttr.Alt]: string | null;
[ImgSizeAttr.Width]: string | null;
[ImgSizeAttr.Height]: string | null;
[ImgSizeAttr.Loading]: string | null;
};

export {ImgSizeAttr};
Expand All @@ -37,6 +38,7 @@ export const ImgSizeSpecs: ExtensionAuto<ImgSizeSpecsOptions> = (builder, opts)
[ImgSizeAttr.Title]: {default: null},
[ImgSizeAttr.Height]: {default: null},
[ImgSizeAttr.Width]: {default: null},
[ImgSizeAttr.Loading]: {default: null},
},
placeholder: placeholderContent ? {content: placeholderContent} : opts.placeholder,
group: 'inline',
Expand All @@ -52,6 +54,9 @@ export const ImgSizeSpecs: ExtensionAuto<ImgSizeSpecsOptions> = (builder, opts)
[ImgSizeAttr.Src]: (dom as Element).getAttribute(ImgSizeAttr.Src),
[ImgSizeAttr.Alt]: (dom as Element).getAttribute(ImgSizeAttr.Alt),
[ImgSizeAttr.Title]: (dom as Element).getAttribute(ImgSizeAttr.Title),
[ImgSizeAttr.Loading]: (dom as Element).getAttribute(
ImgSizeAttr.Loading,
),
[ImgSizeAttr.Height]: isNumber(height) ? height : null,
[ImgSizeAttr.Width]: isNumber(width) ? height : null,
};
Expand All @@ -71,6 +76,7 @@ export const ImgSizeSpecs: ExtensionAuto<ImgSizeSpecsOptions> = (builder, opts)
[ImgSizeAttr.Title]: tok.attrGet(ImgSizeAttr.Title),
[ImgSizeAttr.Height]: tok.attrGet(ImgSizeAttr.Height),
[ImgSizeAttr.Width]: tok.attrGet(ImgSizeAttr.Width),
[ImgSizeAttr.Loading]: tok.attrGet(ImgSizeAttr.Loading),
[ImgSizeAttr.Alt]: tok.children?.[0]?.content || null,
}),
},
Expand Down
Loading