@islands/frontmatter
Advanced tools
Comparing version 0.3.0 to 0.4.0
@@ -9,9 +9,2 @@ import { Plugin } from 'unified'; | ||
declare type FrontmatterPluggable = [FrontmatterPlugin, FrontmatterOptions?]; | ||
/** | ||
* A remark plugin to expose frontmatter data as named exports. | ||
* | ||
* @param options - Optional options to configure the output. | ||
* @returns A unified transformer. | ||
*/ | ||
declare const remarkPlugin: FrontmatterPlugin; | ||
@@ -24,2 +17,2 @@ /** | ||
export { Frontmatter, FrontmatterOptions, FrontmatterPluggable, FrontmatterPlugin, IlesFrontmatter as default, remarkPlugin }; | ||
export { Frontmatter, FrontmatterOptions, FrontmatterPluggable, FrontmatterPlugin, IlesFrontmatter as default }; |
@@ -33,3 +33,3 @@ var __defProp = Object.defineProperty; | ||
// src/remark-plugin.ts | ||
// src/remark-mdx-frontmatter.ts | ||
import estreeUtilValueToEstree from "estree-util-value-to-estree"; | ||
@@ -46,3 +46,3 @@ import { load as yaml } from "js-yaml"; | ||
} | ||
var remarkPlugin = function(options) { | ||
var remarkMdxFrontmatter = function(options) { | ||
const data = this.data(); | ||
@@ -112,2 +112,234 @@ const addExtension = (field, value) => { | ||
// ../../node_modules/.pnpm/unist-util-is@5.1.1/node_modules/unist-util-is/index.js | ||
var convert = function(test) { | ||
if (test === void 0 || test === null) { | ||
return ok; | ||
} | ||
if (typeof test === "string") { | ||
return typeFactory(test); | ||
} | ||
if (typeof test === "object") { | ||
return Array.isArray(test) ? anyFactory(test) : propsFactory(test); | ||
} | ||
if (typeof test === "function") { | ||
return castFactory(test); | ||
} | ||
throw new Error("Expected function, string, or object as test"); | ||
}; | ||
function anyFactory(tests) { | ||
const checks = []; | ||
let index = -1; | ||
while (++index < tests.length) { | ||
checks[index] = convert(tests[index]); | ||
} | ||
return castFactory(any); | ||
function any(...parameters) { | ||
let index2 = -1; | ||
while (++index2 < checks.length) { | ||
if (checks[index2].call(this, ...parameters)) | ||
return true; | ||
} | ||
return false; | ||
} | ||
} | ||
function propsFactory(check) { | ||
return castFactory(all); | ||
function all(node) { | ||
let key; | ||
for (key in check) { | ||
if (node[key] !== check[key]) | ||
return false; | ||
} | ||
return true; | ||
} | ||
} | ||
function typeFactory(check) { | ||
return castFactory(type); | ||
function type(node) { | ||
return node && node.type === check; | ||
} | ||
} | ||
function castFactory(check) { | ||
return assertion; | ||
function assertion(...parameters) { | ||
return Boolean(check.call(this, ...parameters)); | ||
} | ||
} | ||
function ok() { | ||
return true; | ||
} | ||
// ../../node_modules/.pnpm/unist-util-visit-parents@5.1.0/node_modules/unist-util-visit-parents/color.js | ||
function color(d) { | ||
return "[33m" + d + "[39m"; | ||
} | ||
// ../../node_modules/.pnpm/unist-util-visit-parents@5.1.0/node_modules/unist-util-visit-parents/index.js | ||
var CONTINUE = true; | ||
var SKIP = "skip"; | ||
var EXIT = false; | ||
var visitParents = function(tree, test, visitor, reverse) { | ||
if (typeof test === "function" && typeof visitor !== "function") { | ||
reverse = visitor; | ||
visitor = test; | ||
test = null; | ||
} | ||
const is = convert(test); | ||
const step = reverse ? -1 : 1; | ||
factory(tree, null, [])(); | ||
function factory(node, index, parents) { | ||
const value = typeof node === "object" && node !== null ? node : {}; | ||
let name; | ||
if (typeof value.type === "string") { | ||
name = typeof value.tagName === "string" ? value.tagName : typeof value.name === "string" ? value.name : void 0; | ||
Object.defineProperty(visit2, "name", { | ||
value: "node (" + color(value.type + (name ? "<" + name + ">" : "")) + ")" | ||
}); | ||
} | ||
return visit2; | ||
function visit2() { | ||
let result = []; | ||
let subresult; | ||
let offset; | ||
let grandparents; | ||
if (!test || is(node, index, parents[parents.length - 1] || null)) { | ||
result = toResult(visitor(node, parents)); | ||
if (result[0] === EXIT) { | ||
return result; | ||
} | ||
} | ||
if (node.children && result[0] !== SKIP) { | ||
offset = (reverse ? node.children.length : -1) + step; | ||
grandparents = parents.concat(node); | ||
while (offset > -1 && offset < node.children.length) { | ||
subresult = factory(node.children[offset], offset, grandparents)(); | ||
if (subresult[0] === EXIT) { | ||
return subresult; | ||
} | ||
offset = typeof subresult[1] === "number" ? subresult[1] : offset + step; | ||
} | ||
} | ||
return result; | ||
} | ||
} | ||
}; | ||
function toResult(value) { | ||
if (Array.isArray(value)) { | ||
return value; | ||
} | ||
if (typeof value === "number") { | ||
return [CONTINUE, value]; | ||
} | ||
return [value]; | ||
} | ||
// ../../node_modules/.pnpm/unist-util-visit@4.1.0/node_modules/unist-util-visit/index.js | ||
var visit = function(tree, test, visitor, reverse) { | ||
if (typeof test === "function" && typeof visitor !== "function") { | ||
reverse = visitor; | ||
visitor = test; | ||
test = null; | ||
} | ||
visitParents(tree, test, overload, reverse); | ||
function overload(node, parents) { | ||
const parent = parents[parents.length - 1]; | ||
return visitor(node, parent ? parent.children.indexOf(node) : null, parent); | ||
} | ||
}; | ||
// src/remark-mdx-images.ts | ||
var urlPattern = /^(https?:)?\//; | ||
var remarkMdxImages = () => (ast) => { | ||
const imports = []; | ||
const imported = new Map(); | ||
visit(ast, (node, index, parent) => { | ||
if (node.type === "image") | ||
return replaceMarkdownImage(node, index, parent); | ||
if (isJsxElement(node) && node.name === "img") | ||
return replaceSrcAttribute(node); | ||
}); | ||
if (imports.length > 0) | ||
ast.children.unshift(...imports); | ||
function replaceSrcAttribute(node) { | ||
for (const attr of node.attributes) { | ||
if (attr.type === "mdxJsxAttribute" && attr.name === "src" && isString(attr.value)) { | ||
const srcExpression = imageSrcToMdxExpression(attr.value); | ||
if (srcExpression) | ||
attr.value = srcExpression; | ||
break; | ||
} | ||
} | ||
return SKIP; | ||
} | ||
function replaceMarkdownImage(node, index, parent) { | ||
const src = imageSrcToMdxExpression(node.url); | ||
if (src) { | ||
const attrs = { alt: node.alt || null, title: node.title, src }; | ||
if (!node.title) | ||
delete attrs.title; | ||
const mdxImage = { | ||
type: "mdxJsxTextElement", | ||
name: "img", | ||
children: [], | ||
attributes: Object.entries(attrs).map(([name, value]) => ({ type: "mdxJsxAttribute", name, value })) | ||
}; | ||
parent.children.splice(index, 1, mdxImage); | ||
} | ||
return SKIP; | ||
} | ||
function imageSrcToMdxExpression(url) { | ||
const name = imageSrcToIdentifier(url); | ||
if (!name) | ||
return; | ||
return { | ||
type: "mdxJsxAttributeValueExpression", | ||
value: name, | ||
data: { | ||
estree: { | ||
type: "Program", | ||
sourceType: "module", | ||
body: [{ type: "ExpressionStatement", expression: { type: "Identifier", name } }] | ||
} | ||
} | ||
}; | ||
} | ||
function imageSrcToIdentifier(url) { | ||
if (urlPattern.test(url)) | ||
return; | ||
let name = imported.get(url); | ||
if (!name) { | ||
name = `__mdx_image_${imported.size}`; | ||
imported.set(url, name); | ||
imports.push({ | ||
type: "mdxjsEsm", | ||
data: { | ||
estree: { | ||
type: "Program", | ||
sourceType: "module", | ||
body: [ | ||
{ | ||
type: "ImportDeclaration", | ||
source: { type: "Literal", value: url, raw: JSON.stringify(url) }, | ||
specifiers: [ | ||
{ | ||
type: "ImportDefaultSpecifier", | ||
local: { type: "Identifier", name } | ||
} | ||
] | ||
} | ||
] | ||
} | ||
} | ||
}); | ||
} | ||
return name; | ||
} | ||
}; | ||
function isJsxElement(node) { | ||
return node.type === "mdxJsxFlowElement"; | ||
} | ||
function isString(val) { | ||
return typeof val === "string"; | ||
} | ||
// src/frontmatter.ts | ||
@@ -123,5 +355,6 @@ function IlesFrontmatter() { | ||
remarkPlugins: [ | ||
[remarkPlugin, { get extendFrontmatter() { | ||
[remarkMdxFrontmatter, { get extendFrontmatter() { | ||
return extendFrontmatter; | ||
} }] | ||
} }], | ||
remarkMdxImages | ||
] | ||
@@ -132,4 +365,3 @@ } | ||
export { | ||
IlesFrontmatter as default, | ||
remarkPlugin | ||
IlesFrontmatter as default | ||
}; |
{ | ||
"name": "@islands/frontmatter", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "files": [ |
@@ -1,2 +0,3 @@ | ||
import { remarkPlugin } from './remark-plugin' | ||
import { remarkMdxFrontmatter } from './remark-mdx-frontmatter' | ||
import { remarkMdxImages } from './remark-mdx-images' | ||
@@ -8,3 +9,3 @@ export type { | ||
FrontmatterOptions, | ||
} from './remark-plugin' | ||
} from './remark-mdx-frontmatter' | ||
@@ -25,3 +26,4 @@ /** | ||
remarkPlugins: [ | ||
[remarkPlugin, { get extendFrontmatter () { return extendFrontmatter } }], | ||
[remarkMdxFrontmatter, { get extendFrontmatter () { return extendFrontmatter } }], | ||
remarkMdxImages, | ||
], | ||
@@ -31,3 +33,1 @@ }, | ||
} | ||
export { remarkPlugin } |
Sorry, the diff of this file is not supported yet
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
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
22861
9
592
1