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

Added ability for mdxJSX elements to not be stripped out, and also ad… #32

Closed
wants to merge 3 commits into from
Closed
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
133 changes: 129 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,102 @@
return text(state, unsafe)
}

case 'mdxJsxFlowElement': {
/**
* A typedef for the MDX node shape.
* (Adjust property types as needed for your use case.)
*
* @typedef {Object} MdxJsxFlowElement
* @property {string} name
* @property {Record<string, unknown>} [properties]
* @property {Array<{
* type: 'mdxJsxAttribute',
* name: string,
* value: unknown
* }>} [attributes]
*/

/** @type {Record<string, unknown>} */
const attributesMap = {}

/**
* Cast `unsafe` to our `MdxJsxFlowElement`.
* If `unsafe` truly may be something else, you may want extra runtime checks.
*
* @type {MdxJsxFlowElement}
*/
const typedUnsafe = /** @type {MdxJsxFlowElement} */ (unsafe)

if (Array.isArray(typedUnsafe.attributes)) {
for (const attribute of typedUnsafe.attributes) {
if (

Check warning on line 319 in lib/index.js

View workflow job for this annotation

GitHub Actions / lts/hydrogen

Blocks are nested too deeply (5). Maximum allowed is 4.

Check warning on line 319 in lib/index.js

View workflow job for this annotation

GitHub Actions / node

Blocks are nested too deeply (5). Maximum allowed is 4.
attribute &&
typeof attribute === 'object' &&
'name' in attribute &&
'value' in attribute
) {
attributesMap[attribute.name] = attribute.value
}
}
}

/**
* Here we define `mdxProps` to include everything from `typedUnsafe`
* plus a new `tagName` field.
*
* @typedef {MdxJsxFlowElement & {
* tagName: string,
* }} MdxProps
*/

/** @type {MdxProps} */
const mdxProperties = {
...typedUnsafe,
tagName: typedUnsafe.name,
properties: {
...typedUnsafe.properties,
...attributesMap
}
}

return element(state, mdxProperties)
}

case 'mdxJsxTextElement': {
/** @type {Record<string, unknown>} */
const attributesMap = {}

/** @type {{ attributes?: Array<{ type: 'mdxJsxAttribute', name: string, value: unknown }> }} */
const typedUnsafe = unsafe

if (Array.isArray(typedUnsafe.attributes)) {
for (const attribute of typedUnsafe.attributes) {
if (

Check warning on line 361 in lib/index.js

View workflow job for this annotation

GitHub Actions / lts/hydrogen

Blocks are nested too deeply (5). Maximum allowed is 4.

Check warning on line 361 in lib/index.js

View workflow job for this annotation

GitHub Actions / node

Blocks are nested too deeply (5). Maximum allowed is 4.
attribute &&
typeof attribute === 'object' &&
'name' in attribute &&
'value' in attribute
) {
attributesMap[attribute.name] = attribute.value
}
}
}

const mdxProperties = {
.../** @type {{ name: string, properties?: Record<string, unknown> }} */ (
unsafe
),
tagName: /** @type {{ name: string }} */ (unsafe).name,
properties: {
.../** @type {{ properties?: Record<string, unknown> }} */ (unsafe)
.properties,
...attributesMap
}
}

return element(state, mdxProperties)
}

default:
}
}
Expand Down Expand Up @@ -503,17 +599,25 @@
: undefined
const defaults =
attributes && own.call(attributes, '*') ? attributes['*'] : undefined

// Handle both traditional properties and MDX attributes
const properties_ =
/** @type {Readonly<Record<string, Readonly<unknown>>>} */ (
properties && typeof properties === 'object' ? properties : {}
)
properties && typeof properties === 'object' ? properties : {}
/** @type {Array<{ type: 'mdxJsxAttribute' } & Record<string, unknown>>} */
const mdxAttributes =
/** @type {{ attributes?: Array<{ type: 'mdxJsxAttribute' } & Record<string, unknown>> }} */ (
properties_
)?.attributes || []

/** @type {Properties} */
const result = {}
/** @type {string} */
let key

// Process traditional properties
for (key in properties_) {
if (own.call(properties_, key)) {
// @ts-ignore
const unsafe = properties_[key]
let safe = propertyValue(
state,
Expand All @@ -532,9 +636,30 @@
}
}

// Process MDX attributes
for (key in mdxAttributes) {
if (own.call(mdxAttributes, key)) {
const unsafe = mdxAttributes[key]
let safe = propertyValue(
state,
findDefinition(specific, key),
key,
unsafe
)

if (safe === null || safe === undefined) {
safe = propertyValue(state, findDefinition(defaults, key), key, unsafe)
}

if (safe !== null && safe !== undefined) {
result[key] = safe
}
}
}

// Handle required properties
if (required && own.call(required, tagName)) {
const properties = required[tagName]

for (key in properties) {
if (own.call(properties, key) && !own.call(result, key)) {
result[key] = properties[key]
Expand Down
Loading