From 777b8bff6e6944074f388b5cdf74ffaef1d75c49 Mon Sep 17 00:00:00 2001 From: Pedro Nauck Date: Sat, 30 Jun 2018 00:53:10 -0300 Subject: [PATCH] chore(docz-core): remove from output code --- packages/docz-core/package.json | 1 + packages/docz-core/src/types.d.ts | 1 + packages/docz-core/src/utils/plugin-hast.ts | 9 ++++++++- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/docz-core/package.json b/packages/docz-core/package.json index d44d91d65..a4ba8fe40 100644 --- a/packages/docz-core/package.json +++ b/packages/docz-core/package.json @@ -65,6 +65,7 @@ "remark-slug": "^5.0.0", "resolve": "^1.8.1", "signale": "^1.2.1", + "strip-indent": "^2.0.0", "to-vfile": "^5.0.0", "uglifyjs-webpack-plugin": "^1.2.7", "ulid": "^2.3.0", diff --git a/packages/docz-core/src/types.d.ts b/packages/docz-core/src/types.d.ts index 6290e7cd9..8f9d70245 100644 --- a/packages/docz-core/src/types.d.ts +++ b/packages/docz-core/src/types.d.ts @@ -4,6 +4,7 @@ declare module 'chokidar' declare module '@sindresorhus/slugify' declare module 'humanize-string' declare module 'unified' +declare module 'strip-indent' declare module 'unist-util-is' declare module 'unist-util-visit' declare module 'unist-util-find' diff --git a/packages/docz-core/src/utils/plugin-hast.ts b/packages/docz-core/src/utils/plugin-hast.ts index e5f0a07d2..586e10f0e 100644 --- a/packages/docz-core/src/utils/plugin-hast.ts +++ b/packages/docz-core/src/utils/plugin-hast.ts @@ -1,5 +1,6 @@ import is from 'unist-util-is' import nodeToString from 'hast-util-to-string' +import strip from 'strip-indent' import { format } from '../utils/format' @@ -8,6 +9,11 @@ const componentName = (value: any) => { return match && match[1] } +const removePlayground = (code: string) => + code + .replace(/^(\)|(\)$/g, '') + .replace(/\<\/Playground\>/g, '') + const isPlayground = (name: string) => name === 'Playground' const addCodeProp = async (node: any) => { @@ -17,10 +23,11 @@ const addCodeProp = async (node: any) => { if (isPlayground(name)) { const formatted = await format(nodeToString(node)) const code = formatted.slice(1, Infinity) + const child = strip(removePlayground(code)).trim() const codeComponent = `components && ( - {\`${code}\`} + {\`${child}\`} )`