* fix(media_folder_relative): use collection name in unpublished entry * refactor: pass arguments as object to AssetProxy ctor * feat: support media folders per collection * feat: resolve media files path based on entry path * fix: asset public path resolving * refactor: introduce typescript for AssetProxy * refactor: code cleanup * refactor(asset-proxy): add tests,switch to typescript,extract arguments * refactor: typescript for editorialWorkflow * refactor: add typescript for media library actions * refactor: fix type error on map set * refactor: move locale selector into reducer * refactor: add typescript for entries actions * refactor: remove duplication between asset store and media lib * feat: load assets from backend using API * refactor(github): add typescript, cache media files * fix: don't load media URL if already loaded * feat: add media folder config to collection * fix: load assets from API when not in UI state * feat: load entry media files when opening media library * fix: editorial workflow draft media files bug fixes * test(unit): fix unit tests * fix: editor control losing focus * style: add eslint object-shorthand rule * test(cypress): re-record mock data * fix: fix non github backends, large media * test: uncomment only in tests * fix(backend-test): add missing displayURL property * test(e2e): add media library tests * test(e2e): enable visual testing * test(e2e): add github backend media library tests * test(e2e): add git-gateway large media tests * chore: post rebase fixes * test: fix tests * test: fix tests * test(cypress): fix tests * docs: add media_folder docs * test(e2e): add media library delete test * test(e2e): try and fix image comparison on CI * ci: reduce test machines from 9 to 8 * test: add reducers and selectors unit tests * test(e2e): disable visual regression testing for now * test: add getAsset unit tests * refactor: use Asset class component instead of hooks * build: don't inline source maps * test: add more media path tests
68 lines
2.1 KiB
JavaScript
68 lines
2.1 KiB
JavaScript
import React from 'react';
|
|
import { map, has } from 'lodash';
|
|
import { renderToString } from 'react-dom/server';
|
|
import u from 'unist-builder';
|
|
|
|
/**
|
|
* This plugin doesn't actually transform Remark (MDAST) nodes to Rehype
|
|
* (HAST) nodes, but rather, it prepares an MDAST shortcode node for HAST
|
|
* conversion by replacing the shortcode text with stringified HTML for
|
|
* previewing the shortcode output.
|
|
*/
|
|
export default function remarkToRehypeShortcodes({ plugins, getAsset, resolveWidget }) {
|
|
return transform;
|
|
|
|
async function transform(root) {
|
|
const transformedChildren = await Promise.all(map(root.children, processShortcodes));
|
|
return { ...root, children: transformedChildren };
|
|
}
|
|
|
|
/**
|
|
* Mapping function to transform nodes that contain shortcodes.
|
|
*/
|
|
async function processShortcodes(node) {
|
|
/**
|
|
* If the node doesn't contain shortcode data, return the original node.
|
|
*/
|
|
if (!has(node, ['data', 'shortcode'])) return node;
|
|
|
|
/**
|
|
* Get shortcode data from the node, and retrieve the matching plugin by
|
|
* key.
|
|
*/
|
|
const { shortcode, shortcodeData } = node.data;
|
|
const plugin = plugins.get(shortcode);
|
|
|
|
/**
|
|
* Run the shortcode plugin's `toPreview` method, which will return either
|
|
* an HTML string or a React component. If a React component is returned,
|
|
* render it to an HTML string.
|
|
*/
|
|
const value = await getPreview(plugin, shortcodeData);
|
|
const valueHtml = typeof value === 'string' ? value : renderToString(value);
|
|
|
|
/**
|
|
* Return a new 'html' type node containing the shortcode preview markup.
|
|
*/
|
|
const textNode = u('html', valueHtml);
|
|
const children = [textNode];
|
|
return { ...node, children };
|
|
}
|
|
|
|
/**
|
|
* Retrieve the shortcode preview component.
|
|
*/
|
|
async function getPreview(plugin, shortcodeData) {
|
|
const { toPreview, widget } = plugin;
|
|
if (toPreview) {
|
|
return toPreview(shortcodeData, getAsset);
|
|
}
|
|
const preview = resolveWidget(widget);
|
|
return React.createElement(preview.preview, {
|
|
value: shortcodeData,
|
|
field: plugin,
|
|
getAsset,
|
|
});
|
|
}
|
|
}
|