-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] Allow importing full MDX documents
- Loading branch information
Showing
4 changed files
with
109 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
import { DiscoveryData } from "veda"; | ||
import { StoryData } from "veda"; | ||
|
||
export interface DataStoryBlock { | ||
export interface EditorStoryBlock { | ||
id: string; | ||
tag: 'Block' | 'ScrollyTellingBlock'; | ||
blockType?: 'wide' | 'full'; | ||
mdx: string; | ||
} | ||
|
||
export interface DataStory { | ||
frontmatter: DiscoveryData; | ||
blocks: DataStoryBlock[]; | ||
export interface EditorStory { | ||
frontmatter: StoryData; | ||
blocks: EditorStoryBlock[]; | ||
currentBlockId?: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { dump, load } from 'js-yaml'; | ||
import { StoryData } from 'veda'; | ||
import { EditorStory, EditorStoryBlock } from './types'; | ||
|
||
export function toMDXBlock(block: EditorStoryBlock): string { | ||
const type = block.blockType ? ` type=${block.blockType}` : ''; | ||
return `<Block${type}> | ||
${block.mdx} | ||
</Block>`; | ||
} | ||
|
||
export function toMDXDocument(dataStory: EditorStory): string { | ||
const frontmatter = dump(dataStory.frontmatter); | ||
const doc = `--- | ||
${frontmatter} | ||
--- | ||
${dataStory.blocks.map((block) => toMDXBlock(block)).join('\n\n')} | ||
`; | ||
|
||
return doc; | ||
} | ||
|
||
export function toEditorDataStory(mdxDocument: string): EditorStory { | ||
const frontmatterRegex = /^---\n([\s\S]*?)\n---\n([\s\S]*)/; | ||
|
||
const matches = mdxDocument.match(frontmatterRegex); | ||
|
||
if (!matches) { | ||
throw new Error('Frontmatter not found in the Markdown document.'); | ||
} | ||
|
||
const rawFrontmatter = matches[1]; | ||
const rawContent = matches[2]; | ||
|
||
const frontmatter = load(rawFrontmatter) as StoryData; | ||
|
||
const blockRegex = /<Block\s*(?:type=['"](.+?)['"])?\s*>([\s\S]*?)<\/Block>/g; | ||
let blocks: EditorStoryBlock[] = []; | ||
let match; | ||
let id = 0; | ||
while ((match = blockRegex.exec(rawContent)) !== null) { | ||
const blockType = match[1]; | ||
const mdx = match[2].trim(); | ||
blocks = [...blocks, { tag: 'Block', blockType, mdx, id: `${id++}` }]; | ||
} | ||
|
||
return { | ||
frontmatter, | ||
blocks | ||
}; | ||
} |