-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
markdown 内の [alt](path/to/image) から生成されるhtmlタグを改造
<figure> <img src="path/to/image" alt="alt" /> <figcaption>alt</figcaption> </figure>
- Loading branch information
Showing
7 changed files
with
274 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import unified from "unified"; | ||
import { Paragraph } from "mdast"; | ||
import { Node, Parent } from "unist"; | ||
import { VFileCompatible, VFile } from "vfile"; | ||
import { inspect } from "unist-util-inspect"; | ||
import { visit } from "unist-util-visit"; | ||
// ESM port of https://github.com/tkhquang/gridsome-remark-figure-caption | ||
import { whitespace } from "hast-util-whitespace"; | ||
import { remove } from "unist-util-remove"; | ||
import { fromMarkdown } from "mdast-util-from-markdown"; | ||
|
||
|
||
// https://github.com/josestg/rehype-figure | ||
function rehype_figure( | ||
node: any, // this is mdx node | ||
index: number, | ||
parent: Parent | undefined | ||
) { | ||
if (node.type != 'mdxJsxTextElement') { | ||
return; | ||
} | ||
if (node.name == 'img') { | ||
return; | ||
} | ||
// if(node.tagName!="img"){ | ||
// return; | ||
// } | ||
console.log(inspect(node)); | ||
} | ||
|
||
// [unified を使って Markdown を拡張する](https://zenn.dev/januswel/articles/745787422d425b01e0c1) | ||
const nop: unified.Plugin = () => { | ||
return (tree: Node, file: VFileCompatible) => { | ||
// @ts-ignore | ||
const path = file.path; | ||
if (!path) { | ||
console.log(inspect(file)); | ||
return; | ||
} | ||
if (!path.endsWith("univrm_export.md")) { | ||
// debug | ||
console.log(file, inspect(tree)); | ||
return; | ||
} | ||
|
||
// process | ||
console.log(inspect(tree)); | ||
// @type-ignore | ||
visit(tree, rehype_figure); | ||
} | ||
}; | ||
export default nop | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
// from https://github.com/Microflash/remark-figure-caption | ||
// | ||
// ESM port of https://github.com/tkhquang/gridsome-remark-figure-caption | ||
import { visit } from "unist-util-visit"; | ||
import { whitespace } from "hast-util-whitespace"; | ||
import { remove } from "unist-util-remove"; | ||
import { fromMarkdown } from "mdast-util-from-markdown"; | ||
|
||
const createNodes = (imageNode, { figureClassName, imageClassName, captionClassName }) => { | ||
let figchildren = null; | ||
try { | ||
figchildren = fromMarkdown(imageNode.alt).children.flatMap(node => node.children); | ||
} catch (e) { | ||
console.log(`figure-caption-plugin: Failed to parse image alt-text as markdown - using raw value as fallback: ${imageNode.alt}`); | ||
figchildren = [ | ||
{ | ||
type: "text", | ||
value: imageNode.alt, | ||
}, | ||
]; | ||
} | ||
|
||
const figcaption = { | ||
type: "figcaption", | ||
children: figchildren, | ||
data: { | ||
hName: "figcaption", | ||
...getClassProp(captionClassName), | ||
}, | ||
}; | ||
|
||
const figure = { | ||
type: "figure", | ||
children: [getImageNodeWithClasses(imageNode, imageClassName), figcaption], | ||
data: { | ||
hName: "figure", | ||
...getClassProp(figureClassName), | ||
}, | ||
}; | ||
|
||
return figure; | ||
}; | ||
|
||
const hasOnlyImages = (node) => { | ||
return node.children.every((child) => { | ||
return child.type === "image" || whitespace(child); | ||
}); | ||
}; | ||
|
||
const isImageNodeWithAlt = (node) => { | ||
return node.type === "image" && Boolean(node.alt) && Boolean(node.url); | ||
}; | ||
|
||
const isHTMLImageNode = (node) => { | ||
return node.type === "html" && Boolean(node.alt) && /^<img\s/.test(node.value); | ||
}; | ||
|
||
const isImageWithAlt = (node) => { | ||
return isImageNodeWithAlt(node) || isHTMLImageNode(node); | ||
}; | ||
|
||
const isImageWithCaption = (parent) => { | ||
return ( | ||
parent.type === "figure" && | ||
parent.children.some((child) => child.type === "figcaption") | ||
); | ||
}; | ||
|
||
const isImageLink = (parent) => { | ||
return (parent.type === "link"); | ||
}; | ||
|
||
const getClassProp = (className) => { | ||
return { | ||
...(className && { | ||
hProperties: { | ||
class: [className], | ||
}, | ||
}), | ||
}; | ||
}; | ||
|
||
const classRegex = /\sclass="(.*?)"\s/gi; | ||
|
||
const getImageNodeWithClasses = (node, classes) => { | ||
// Is Image type node | ||
if (!isHTMLImageNode(node)) { | ||
return { | ||
...node, | ||
data: { | ||
...getClassProp(classes), | ||
}, | ||
}; | ||
} | ||
|
||
// is HTML Image node | ||
if (!classes) { | ||
return { | ||
...node, | ||
}; | ||
} | ||
|
||
// Bruteforce adding classes for now | ||
const hasClass = classRegex.exec(node.value); | ||
|
||
if (!hasClass) { | ||
return { | ||
...node, | ||
value: node.value.replace(/<img\s/, `<img class="${classes}" `), | ||
}; | ||
} | ||
|
||
return { | ||
...node, | ||
value: node.value.replace(classRegex, ` class="$1 ${classes}" `), | ||
}; | ||
}; | ||
|
||
/** @type {import("unified").Plugin<[], import("mdast").Root>} */ | ||
function remarkFigureCaption(options = {}) { | ||
return (tree) => { | ||
// Unwrap the images inside Paragraphs | ||
visit(tree, "paragraph", (node, index, parent) => { | ||
if (!hasOnlyImages(node)) { | ||
return; | ||
} | ||
|
||
remove(node, "text"); | ||
|
||
parent.children.splice(index, 1, ...node.children); | ||
|
||
return index; | ||
}); | ||
|
||
// Wrap image nodes in figure | ||
visit( | ||
tree, | ||
(node) => isImageWithAlt(node), | ||
(node, index, parent) => { | ||
// console.log(node.type, node.name); | ||
if (isImageWithCaption(parent) || isImageLink(parent)) { | ||
return; | ||
} | ||
|
||
const figure = createNodes(node, options); | ||
|
||
node.type = figure.type; | ||
node.children = figure.children; | ||
node.data = figure.data; | ||
} | ||
); | ||
}; | ||
} | ||
|
||
export default remarkFigureCaption; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters