
Product
Socket Now Protects the Chrome Extension Ecosystem
Socket is launching experimental protection for Chrome extensions, scanning for malware and risky permissions to prevent silent supply chain attacks.
@mdjs/mdjs-preview
Advanced tools
Rendering storybook story functions inside a preview window with show code capabilities
/* START - Rocket auto generated - do not touch */
export const sourceRelativeFilePath = '30--tools/20--markdown-javascript/20--preview.rocket.md';
// prettier-ignore
import { html, layout, setupUnifiedPlugins, components, openGraphLayout } from '../../recursive.data.js';
export { html, layout, setupUnifiedPlugins, components, openGraphLayout };
export async function registerCustomElements() {
// server-only components
// prettier-ignore
customElements.define('rocket-social-link', await import('@rocket/components/social-link.js').then(m => m.RocketSocialLink));
// prettier-ignore
customElements.define('rocket-header', await import('@rocket/components/header.js').then(m => m.RocketHeader));
// prettier-ignore
customElements.define('inline-notification', await import('@rocket/components/inline-notification.js').then(m => m.InlineNotification));
// prettier-ignore
customElements.define('rocket-main-docs', await import('@rocket/components/main-docs.js').then(m => m.RocketMainDocs));
// prettier-ignore
customElements.define('rocket-content-area', await import('@rocket/components/content-area.js').then(m => m.RocketContentArea));
// hydrate-able components
// prettier-ignore
customElements.define('rocket-search', await import('@rocket/search/search.js').then(m => m.RocketSearch));
// prettier-ignore
customElements.define('rocket-drawer', await import('@rocket/components/drawer.js').then(m => m.RocketDrawer));
}
export const needsLoader = true;
/* END - Rocket auto generated - do not touch */
You can showcase live running code by annotating a code block with js preview-story
.
import { html } from '@mdjs/mdjs-preview';
import './assets/demo-element.js';
```js client
import { html } from '@mdjs/mdjs-preview';
import './assets/demo-element.js';
```
```js preview-story
export const foo = () => html`<demo-element></demo-element>`;
```
will result in
export const foo = () => html` <demo-element></demo-element> `;
// not defined for android
// not defined for ios
If your preview is followed by a code blocks marked as story-code
then those will be shown when switching between multiple platforms
```js preview-story
// will be visible when platform web is selected
export const JsPreviewStory = () => html` <demo-element></demo-element> `;
```
```xml story-code
<!-- will be visible when platform android is selected -->
<Button
android:id="@+id/demoElement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android Code"
style="@style/Widget.Demo.Element"
/>
```
```swift story-code
// will be visible when platform ios is selected
import Demo.Element
let card = DemoElement()
```
See it in action by opening up the code block and switching platforms
// will be visible when platform web is selected
export const JsPreviewStory = () => html` <demo-element></demo-element> `;
<!-- will be visible when platform android is selected -->
<Button
android:id="@+id/demoElement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android Code"
style="@style/Widget.Demo.Element"
/>
// will be visible when platform ios is selected
import Demo.Element
let card = DemoElement()
```html preview-story
<demo-element></demo-element>
```
will result in
<demo-element></demo-element>
For simulation mode we need a dedicated html file that will be used as an iframe target while loading stories.
The fastest way to create such a file is to use the layout-simulator
layout.
Create a file docs/simulator.md
with the following content.
---
layout: layout-simulator
eleventyExcludeFromCollections: true
excludeFromSearch: true
---
Once you have that you need to configure it for the story renderer by setting it in your config/rocket.config.js
.
/** @type {import('rocket/cli').RocketCliConfig} */
export default ({
setupUnifiedPlugins: [
adjustPluginOptions('mdjsSetupCode', {
simulationSettings: { simulatorUrl: '/simulator/' },
}),
],
});
You can freely choose the path for the "simulator" by creating the md file in a different folder and adjusting the path in the config.
To simulate these stats that usually come from the device itself we put those infos on the document tag.
We can simulate the following settings
platform
Adopting styles and behavior depending on which device platform you are.
<html platform="web"></html>
<html platform="android"></html>
<html platform="ios"></html>
<!-- potentially later -->
<html platform="web-windows"></html>
<html platform="web-mac"></html>
theme
Adjust your styles based on a theme - light/dark are the default but you can add as many as you want.
<html theme="light"></html>
<html theme="dark"></html>
language
Best to relay on data-lang
as lang
often gets changes by translations services which may interfere with your translation loading system.
<html lang="en-US" data-lang="en-US"></html>
<html lang="de-DE" data-lang="de-DE"></html>
If you want to react to such document changes you can use an MutationObserver.
For a vanilla web component it could look something like this:
class DemoElement extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: 'open' });
this.platform = 'the web';
this.language = 'en-US';
this.theme = 'light';
this.observer = new MutationObserver(this.updateData);
}
updateData = () => {
this.platform = document.documentElement.getAttribute('platform') || 'the web';
this.language = document.documentElement.getAttribute('data-lang') || 'en-US';
this.theme = document.documentElement.getAttribute('theme') || 'light';
this.requestUpdate();
};
connectedCallback() {
this.updateData();
this.observer.observe(document.documentElement, { attributes: true });
}
requestUpdate() {
this.shadowRoot.innerHTML = this.render();
}
render() {
return `
...
`;
}
}
customElements.define('demo-element', DemoElement);
It is possible to define a custom version of mdjs-preview in order to add functionality, change
its appearance of make it run in 'hybrid mode' (accepting both lit1 and -2 TemplateResults).
The example below shows how the latter can be achieved by providing a custom render function.
Note that we define mdjs-preview
as the custom element name. We need to make sure that this
file is executed before the original mdjs-preview definition file is executed.
import { MdJsPreview } from '@mdjs/mdjs-preview';
import { render as render2 } from 'lit';
import { isTemplateResult as isTemplateResult2 } from 'lit/directive-helpers.js';
import { render as render1 } from 'lit-html';
export class HybridLitMdjsPreview extends MdJsPreview {
renderStory(html, container, options) {
if (isTemplateResult2(html)) {
render2(html, container, options);
} else {
render1(html, container, options);
}
}
customElements.define('mdjs-preview', HybridLitMdjsPreview);
FAQs
Rendering storybook story functions inside a preview window with show code capabilities
The npm package @mdjs/mdjs-preview receives a total of 4,139 weekly downloads. As such, @mdjs/mdjs-preview popularity was classified as popular.
We found that @mdjs/mdjs-preview demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket is launching experimental protection for Chrome extensions, scanning for malware and risky permissions to prevent silent supply chain attacks.
Product
Add secure dependency scanning to Claude Desktop with Socket MCP, a one-click extension that keeps your coding conversations safe from malicious packages.
Product
Socket now supports Scala and Kotlin, bringing AI-powered threat detection to JVM projects with easy manifest generation and fast, accurate scans.