From 609ae1671d6176a50f7fe187bba701205a0e32bf Mon Sep 17 00:00:00 2001 From: Farnabaz Date: Mon, 17 May 2021 20:24:39 +0430 Subject: [PATCH] fix: fully support YAML structure inside directives --- docs/pages/templates/pre-launch.md | 8 +++--- src/core/parser/markdown/plugin/directive.ts | 25 +++++-------------- .../components/organisms/PreLaunchHero.vue | 24 ++++++++---------- 3 files changed, 21 insertions(+), 36 deletions(-) diff --git a/docs/pages/templates/pre-launch.md b/docs/pages/templates/pre-launch.md index ad8afc23a..832199365 100644 --- a/docs/pages/templates/pre-launch.md +++ b/docs/pages/templates/pre-launch.md @@ -3,10 +3,12 @@ template: pre-launch title: Pre-launch template --- + :::pre-launch-hero{yml} title: Awesome startup description: Pretty long awesome startup description -ctaDescription: Request an invite -ctaPlaceholder: Your email -ctaLabel: Get Invite +cta: + description: Request an invite + placeholder: Your email + label: Get Invite ::: diff --git a/src/core/parser/markdown/plugin/directive.ts b/src/core/parser/markdown/plugin/directive.ts index 339361e38..1a94d312b 100644 --- a/src/core/parser/markdown/plugin/directive.ts +++ b/src/core/parser/markdown/plugin/directive.ts @@ -1,4 +1,3 @@ -import hastToMarkdown from 'mdast-util-to-markdown' import visit from 'unist-util-visit' import h from 'hastscript' import { useMarkdownParser } from '../' @@ -8,24 +7,11 @@ ${yamlString} ---` export default function htmlDirectives({ directives, dataComponents }) { - const toMarkdownExtensions = this.data().toMarkdownExtensions const parser = useMarkdownParser() - function toMarkdown(node) { - return node.children - .map(child => - hastToMarkdown(child, { - bullet: '-', - extensions: toMarkdownExtensions - }) - ) - .join('') - } - - function toData(node) { - const markdown = toMarkdown(node) - // fix issue with toMarkdown autolinker - .replace(/<(https?:[^>]*)>/g, '$1') + function toData(raw) { + const lines = raw.split('\n') + const markdown = lines.slice(1, lines.length - 1).join('\n') const { data } = parser.parseFrontMatter(toFrontMatter(markdown)) @@ -45,7 +31,7 @@ export default function htmlDirectives({ directives, dataComponents }) { return Object.fromEntries(enteries) } - return async (tree, { data: pageData }) => { + return async (tree, { data: pageData, contents }) => { const jobs = [] visit(tree, ['textDirective', 'leafDirective', 'containerDirective'], visitor) @@ -55,10 +41,11 @@ export default function htmlDirectives({ directives, dataComponents }) { const hast = h(node.name, node.attributes) if (dataComponents.includes(node.name) || typeof node.attributes.yml !== 'undefined') { + const { start, end } = node.position hast.properties = bindData( { ...hast.properties, - ...toData(node) + ...toData(contents.substr(start.offset, end.offset - start.offset)) }, pageData ) diff --git a/src/defaultTheme/components/organisms/PreLaunchHero.vue b/src/defaultTheme/components/organisms/PreLaunchHero.vue index b9f7500c5..7794ad1a9 100644 --- a/src/defaultTheme/components/organisms/PreLaunchHero.vue +++ b/src/defaultTheme/components/organisms/PreLaunchHero.vue @@ -22,20 +22,20 @@

- {{ ctaDescription }} + {{ cta.description }}

@@ -67,17 +67,13 @@ export default defineComponent({ type: String, default: 'I am the Hero description, with some text useful to go with the title.' }, - ctaDescription: { - type: String, - default: 'CTA description' - }, - ctaPlaceholder: { - type: String, - default: 'CTA placeholder' - }, - ctaLabel: { - type: String, - default: 'CTA label' + cta: { + type: Object, + default: () => ({ + label: 'CTA label', + description: 'CTA description', + placeholder: 'CTA placeholder' + }) }, imageUrl: { type: String,