@islands/frontmatter
Advanced tools
Comparing version 0.4.0 to 0.4.1
@@ -1,2 +0,2 @@ | ||
import { Plugin } from 'unified'; | ||
import { VFile } from 'vfile'; | ||
@@ -7,5 +7,7 @@ declare type Frontmatter = Record<string, any>; | ||
} | ||
declare type FrontmatterPlugin = Plugin<[FrontmatterOptions?]>; | ||
declare type FrontmatterPluggable = [FrontmatterPlugin, FrontmatterOptions?]; | ||
interface ImageOptions { | ||
withImageSrc?: (src: string, file: VFile) => string | void; | ||
} | ||
/** | ||
@@ -17,2 +19,2 @@ * An iles module that injects remark plugins to parse frontmatter and expose it | ||
export { Frontmatter, FrontmatterOptions, FrontmatterPluggable, FrontmatterPlugin, IlesFrontmatter as default }; | ||
export { Frontmatter, FrontmatterOptions, ImageOptions, IlesFrontmatter as default }; |
@@ -249,13 +249,14 @@ var __defProp = Object.defineProperty; | ||
var urlPattern = /^(https?:)?\//; | ||
var remarkMdxImages = () => (ast) => { | ||
var remarkMdxImages = (options) => (ast, vfile) => { | ||
const parent = ast; | ||
const imports = []; | ||
const imported = new Map(); | ||
visit(ast, (node, index, parent) => { | ||
visit(parent, (node, index, parent2) => { | ||
if (node.type === "image") | ||
return replaceMarkdownImage(node, index, parent); | ||
if (isJsxElement(node) && node.name === "img") | ||
return replaceMarkdownImage(node, index, parent2); | ||
if (isJsxElement(node) && (node.name === "img" || node.name === "Img" || node.name === "Image")) | ||
return replaceSrcAttribute(node); | ||
}); | ||
if (imports.length > 0) | ||
ast.children.unshift(...imports); | ||
parent.children.unshift(...imports); | ||
function replaceSrcAttribute(node) { | ||
@@ -272,3 +273,3 @@ for (const attr of node.attributes) { | ||
} | ||
function replaceMarkdownImage(node, index, parent) { | ||
function replaceMarkdownImage(node, index, parent2) { | ||
const src = imageSrcToMdxExpression(node.url); | ||
@@ -285,3 +286,3 @@ if (src) { | ||
}; | ||
parent.children.splice(index, 1, mdxImage); | ||
parent2.children.splice(index, 1, mdxImage); | ||
} | ||
@@ -307,2 +308,3 @@ return SKIP; | ||
function imageSrcToIdentifier(url) { | ||
var _a; | ||
if (urlPattern.test(url)) | ||
@@ -314,2 +316,3 @@ return; | ||
imported.set(url, name); | ||
const src = ((_a = options == null ? void 0 : options.withImageSrc) == null ? void 0 : _a.call(options, url, vfile)) || url; | ||
imports.push({ | ||
@@ -324,3 +327,3 @@ type: "mdxjsEsm", | ||
type: "ImportDeclaration", | ||
source: { type: "Literal", value: url, raw: JSON.stringify(url) }, | ||
source: { type: "Literal", value: src, raw: JSON.stringify(src) }, | ||
specifiers: [ | ||
@@ -350,14 +353,12 @@ { | ||
function IlesFrontmatter() { | ||
let extendFrontmatter; | ||
let options = {}; | ||
return { | ||
name: "@islands/frontmatter", | ||
configResolved({ markdown }) { | ||
extendFrontmatter = markdown.extendFrontmatter; | ||
Object.assign(options, markdown); | ||
}, | ||
markdown: { | ||
remarkPlugins: [ | ||
[remarkMdxFrontmatter, { get extendFrontmatter() { | ||
return extendFrontmatter; | ||
} }], | ||
remarkMdxImages | ||
[remarkMdxFrontmatter, options], | ||
[remarkMdxImages, options] | ||
] | ||
@@ -364,0 +365,0 @@ } |
{ | ||
"name": "@islands/frontmatter", | ||
"version": "0.4.0", | ||
"version": "0.4.1", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "files": [ |
@@ -1,7 +0,5 @@ | ||
import { remarkMdxFrontmatter } from './remark-mdx-frontmatter' | ||
import { remarkMdxImages } from './remark-mdx-images' | ||
import { remarkMdxFrontmatter, FrontmatterOptions } from './remark-mdx-frontmatter' | ||
import { remarkMdxImages, ImageOptions } from './remark-mdx-images' | ||
export type { | ||
FrontmatterPluggable, | ||
FrontmatterPlugin, | ||
Frontmatter, | ||
@@ -11,2 +9,6 @@ FrontmatterOptions, | ||
export type { | ||
ImageOptions, | ||
} from './remark-mdx-images' | ||
/** | ||
@@ -17,3 +19,3 @@ * An iles module that injects remark plugins to parse frontmatter and expose it | ||
export default function IlesFrontmatter (): any { | ||
let extendFrontmatter: any | ||
let options: FrontmatterOptions & ImageOptions = {} | ||
@@ -23,8 +25,8 @@ return { | ||
configResolved ({ markdown }: any) { | ||
extendFrontmatter = markdown.extendFrontmatter | ||
Object.assign(options, markdown) | ||
}, | ||
markdown: { | ||
remarkPlugins: [ | ||
[remarkMdxFrontmatter, { get extendFrontmatter () { return extendFrontmatter } }], | ||
remarkMdxImages, | ||
[remarkMdxFrontmatter, options], | ||
[remarkMdxImages, options], | ||
], | ||
@@ -31,0 +33,0 @@ }, |
@@ -19,5 +19,2 @@ import type { Program, VariableDeclarator } from 'estree' | ||
export type FrontmatterPlugin = Plugin<[FrontmatterOptions?]> | ||
export type FrontmatterPluggable = [FrontmatterPlugin, FrontmatterOptions?] | ||
function mapFind <T, O> (arr: T[], fn: (i: T) => O): O | undefined { | ||
@@ -35,3 +32,3 @@ let result | ||
*/ | ||
export const remarkMdxFrontmatter: FrontmatterPlugin = function (options?: FrontmatterOptions) { | ||
export const remarkMdxFrontmatter: Plugin<[FrontmatterOptions?]> = function (options?: FrontmatterOptions) { | ||
const data = this.data() | ||
@@ -38,0 +35,0 @@ |
@@ -6,2 +6,3 @@ import type { MDXJsxFlowElement, MDXJsxTextElement, MDXJsxAttribute, MDXJsxAttributeValueExpression } from 'mdast-util-mdx-jsx' | ||
import type { Parent, Node } from 'unist' | ||
import type { VFile } from 'vfile' | ||
@@ -12,2 +13,6 @@ import { visit, SKIP } from 'unist-util-visit' | ||
export interface ImageOptions { | ||
withImageSrc?: (src: string, file: VFile) => string | void | ||
} | ||
/** | ||
@@ -17,11 +22,12 @@ * A Remark plugin for converting Markdown images to MDX images using imports | ||
*/ | ||
export const remarkMdxImages = () => (ast: Parent) => { | ||
export const remarkMdxImages: Plugin<[ImageOptions?]> = (options?: ImageOptions) => (ast, vfile) => { | ||
const parent = ast as Parent | ||
const imports: Omit<MDXJSEsm, 'value'>[] = [] | ||
const imported = new Map<string, string>() | ||
visit(ast, (node, index, parent) => { | ||
visit(parent, (node, index, parent) => { | ||
if (node.type === 'image') | ||
return replaceMarkdownImage(node as Image, index!, parent!) | ||
if (isJsxElement(node) && node.name === 'img') | ||
if (isJsxElement(node) && (node.name === 'img' || node.name === 'Img' || node.name === 'Image')) | ||
return replaceSrcAttribute(node) | ||
@@ -31,3 +37,3 @@ }) | ||
if (imports.length > 0) | ||
ast.children.unshift(...imports) | ||
parent.children.unshift(...imports) | ||
@@ -89,2 +95,5 @@ function replaceSrcAttribute (node: MDXJsxFlowElement) { | ||
imported.set(url, name) | ||
const src = options?.withImageSrc?.(url, vfile) || url | ||
imports.push({ | ||
@@ -99,3 +108,3 @@ type: 'mdxjsEsm', | ||
type: 'ImportDeclaration', | ||
source: { type: 'Literal', value: url, raw: JSON.stringify(url) }, | ||
source: { type: 'Literal', value: src, raw: JSON.stringify(src) }, | ||
specifiers: [ | ||
@@ -102,0 +111,0 @@ { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
23165
599
0