From 78ea9bffb4992eec2290e31a9d086b40b0f9304b Mon Sep 17 00:00:00 2001 From: Shinyu Murakami Date: Fri, 10 Mar 2023 07:33:12 +0900 Subject: [PATCH] feat: disable img attributes copy to figure element (#167) - resolves #151 --- docs/ja/vfm.md | 16 ++++++++-------- docs/vfm.md | 16 ++++++++-------- src/plugins/figure.ts | 41 ----------------------------------------- tests/figure.test.ts | 2 +- 4 files changed, 17 insertions(+), 58 deletions(-) diff --git a/docs/ja/vfm.md b/docs/ja/vfm.md index b352f4a..6ed893a 100644 --- a/docs/ja/vfm.md +++ b/docs/ja/vfm.md @@ -378,8 +378,6 @@ img { 単一行で書かれた画像はキャプション付きで `
` 内へ包み込みます。 -画像の属性を指定した場合、`id` は `
` へ移動され ` ` 固有のもの (`src` など) を除いたすべてがコピーされます。 - **VFM** ```md @@ -394,14 +392,16 @@ text ![Figure 3](./fig3.png) ```html
- Figure 1 -
Figure 1
+ Figure 1 +
-
- caption -
Figure 2
+
+ Figure 2 +
-

text Figure 3

+

text + Figure 3 +

``` **CSS** diff --git a/docs/vfm.md b/docs/vfm.md index c6eae76..068017c 100644 --- a/docs/vfm.md +++ b/docs/vfm.md @@ -379,8 +379,6 @@ img { Wraps an image written as a single line and with a caption in `
`. -If specify attributes for the image, the `id` is moved to `
` and everything else is copied except for` `specific (such as `src`). - **VFM** ```md @@ -395,14 +393,16 @@ text ![Figure 3](./fig3.png) ```html
- Figure 1 -
Figure 1
+ Figure 1 +
-
- caption -
Figure 2
+
+ Figure 2 +
-

text Figure 3

+

text + Figure 3 +

``` **CSS** diff --git a/src/plugins/figure.ts b/src/plugins/figure.ts index d29b3d2..93a6740 100644 --- a/src/plugins/figure.ts +++ b/src/plugins/figure.ts @@ -4,39 +4,10 @@ import h from 'hastscript'; import { Node, Parent } from 'unist'; import visit from 'unist-util-visit'; -/** - * Check if the specified property is `` specific. - * @see https://html.spec.whatwg.org/multipage/embedded-content.html#the-img-element - * @param name Property name. - * @returns If the specified property is `true`. - */ -const isImgProperty = (name: string): boolean => { - switch (name) { - case 'alt': - case 'src': - case 'srcset': - case 'sizes': - case 'crossorigin': - case 'usemap': - case 'ismap': - case 'width': - case 'height': - case 'referrerpolicy': - case 'decoding': - case 'loading': - return true; - - default: - return false; - } -}; - /** * Wrap the single line `` in `
` and generate `
` from the `alt` attribute. * * A single line `` is a child of `

` with no sibling elements. Also, `

` cannot be a child of `

`. So convert the parent `

` to `

`. - * - * Also, of the attributes of ``,` id` is moved to `
`, and the others are copied except for `` specific (such as `src`). * @param img `` tag. * @param parent `

` tag. */ @@ -49,18 +20,6 @@ const wrapFigureImg = (img: Element, parent: Element) => { parent.children.push( h('figcaption', { 'aria-hidden': 'true' }, [img.properties.alt]), ); - - // Move to parent because `id` attribute is unique - if (img.properties.id) { - parent.properties.id = img.properties.id; - delete img.properties.id; - } - - for (const key of Object.keys(img.properties)) { - if (!isImgProperty(key)) { - parent.properties[key] = img.properties[key]; - } - } }; export const hast = () => (tree: Node) => { diff --git a/tests/figure.test.ts b/tests/figure.test.ts index 207ba06..58a87e4 100644 --- a/tests/figure.test.ts +++ b/tests/figure.test.ts @@ -75,6 +75,6 @@ it( alt: "caption" data: {"hProperties":{"id":"image","data-sample":"sample"}} `, - `

caption
`, + `
caption
`, ), );