From 3e9ec74284d8e872f63c4d879af6f8a383c929e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Bonnet?= Date: Thu, 12 Nov 2020 19:46:25 +0100 Subject: [PATCH] Add helper for code block --- .../markdown/src/doxygen/graphType.spec.ts | 12 ++++-------- packages/markdown/src/doxygen/graphType.ts | 8 ++------ .../markdown/src/doxygen/listingType.spec.ts | 18 ++++++------------ packages/markdown/src/doxygen/listingType.ts | 8 ++------ .../src/doxygen/memberdefType/define.ts | 13 +++++++------ .../markdown/src/doxygen/memberdefType/enum.ts | 10 ++++++---- .../src/doxygen/memberdefType/event.ts | 5 ++--- .../src/doxygen/memberdefType/function.ts | 6 ++---- .../src/doxygen/memberdefType/property.ts | 5 ++--- .../src/doxygen/memberdefType/typedef.ts | 5 ++--- .../src/doxygen/memberdefType/variable.ts | 8 +++++--- packages/markdown/src/helpers/index.ts | 8 +++++++- 12 files changed, 47 insertions(+), 59 deletions(-) diff --git a/packages/markdown/src/doxygen/graphType.spec.ts b/packages/markdown/src/doxygen/graphType.spec.ts index 943a125..b56a2f0 100644 --- a/packages/markdown/src/doxygen/graphType.spec.ts +++ b/packages/markdown/src/doxygen/graphType.spec.ts @@ -15,12 +15,10 @@ describe('graphType', () => { specify('empty', () => { const xml = ``; - const md = ` -\`\`\`mermaid + const md = `\`\`\`mermaid graph LR -\`\`\` -`; +\`\`\``; expect(render('LR', false)(xml)).to.equal(md); }); specify('full example', () => { @@ -38,8 +36,7 @@ graph LR `; - const md = ` -\`\`\`mermaid + const md = `\`\`\`mermaid graph LR 1["first node"] click 1 "file_12345.md#file_12345" @@ -51,8 +48,7 @@ click 1 "file_12345.md#file_12345" click 2 "file_67890.md#file_67890" 2 --> 3 -\`\`\` -`; +\`\`\``; expect(render('LR', false)(xml)).to.equal(md); }); }); diff --git a/packages/markdown/src/doxygen/graphType.ts b/packages/markdown/src/doxygen/graphType.ts index 012ce39..62c0dab 100644 --- a/packages/markdown/src/doxygen/graphType.ts +++ b/packages/markdown/src/doxygen/graphType.ts @@ -9,6 +9,7 @@ import { Element } from '@rgrove/parse-xml'; import { Mappers, applyToChildren } from '../mappers'; +import { codeBlock } from '../helpers'; import nodeType from './nodeType'; const mappers = (reverse: boolean): Mappers => ({ @@ -20,12 +21,7 @@ const graphType = (direction: string, reverse: boolean) => ( ) => { const nodes = applyToChildren(mappers(reverse))(element); - return ` -\`\`\`mermaid -graph ${direction} -${nodes.join('\n')} -\`\`\` -`; + return codeBlock('mermaid', `graph ${direction}\n${nodes.join('\n')}`); }; export default graphType; diff --git a/packages/markdown/src/doxygen/listingType.spec.ts b/packages/markdown/src/doxygen/listingType.spec.ts index 6fc674d..1a3f69a 100644 --- a/packages/markdown/src/doxygen/listingType.spec.ts +++ b/packages/markdown/src/doxygen/listingType.spec.ts @@ -16,11 +16,9 @@ describe('listingType', () => { specify('empty', () => { const xml = ``; - const md = ` -\`\`\` + const md = `\`\`\` -\`\`\` -`; +\`\`\``; expect(render(xml)).to.equal(md); }); @@ -29,12 +27,10 @@ describe('listingType', () => { Basic Normal/*Comment*/ `; - const md = ` -\`\`\` + const md = `\`\`\` Basic Normal /* Comment */ -\`\`\` -`; +\`\`\``; expect(render(xml)).to.equal(md); }); @@ -50,12 +46,10 @@ Normal /* Comment */ Basic Normal/*Comment*/ `; - const md = ` -\`\`\`cpp + const md = `\`\`\`cpp Basic Normal /* Comment */ -\`\`\` -`; +\`\`\``; expect(render(xml)).to.equal(md); }); }); diff --git a/packages/markdown/src/doxygen/listingType.ts b/packages/markdown/src/doxygen/listingType.ts index cbbf210..6d9bd8b 100644 --- a/packages/markdown/src/doxygen/listingType.ts +++ b/packages/markdown/src/doxygen/listingType.ts @@ -12,7 +12,7 @@ import { Element } from '@rgrove/parse-xml'; import { currentContext } from '@seaborg/core/lib/services'; import { Mappers, applyToChildren } from '../mappers'; -import { languageCode } from '../helpers'; +import { codeBlock, languageCode } from '../helpers'; import { codelineType } from '.'; const mappers = (): Mappers => ({ @@ -23,9 +23,5 @@ export default (element: Element) => { const { language } = currentContext(); const lines = applyToChildren(mappers())(element); - return ` -\`\`\`${language ? languageCode(language) : ''} -${lines.join('\n')} -\`\`\` -`; + return codeBlock(languageCode(language), lines.join('\n')); }; diff --git a/packages/markdown/src/doxygen/memberdefType/define.ts b/packages/markdown/src/doxygen/memberdefType/define.ts index 0c7f166..2bbc927 100644 --- a/packages/markdown/src/doxygen/memberdefType/define.ts +++ b/packages/markdown/src/doxygen/memberdefType/define.ts @@ -3,6 +3,7 @@ import { Element } from '@rgrove/parse-xml'; import { Mappers, applyToChildrenGrouped, $default } from '../../mappers'; import { xsdString } from '../../generic'; import { + codeBlock, joinParagraphs, languageCode, md, @@ -37,12 +38,12 @@ const template = ({ joinParagraphs([ memberdefTitle(id, `${memberLabel(kind)} ${md(name)}`), memberdefBadges(context), - location, - `\`\`\`${languageCode(language)} -#define ${name}${argsstring || ''}${initializer ? ` ${initializer}` : ''}${ - param ? `( ${param.join(' ,')} )` : '' - } -\`\`\``, + codeBlock( + languageCode(language), + `#define ${name}${argsstring || ''}${ + initializer ? ` ${initializer}` : '' + }${param ? `( ${param.join(' ,')} )` : ''}` + ), memberdefDescription(context), section('Return type', type), memberdefReferences(context), diff --git a/packages/markdown/src/doxygen/memberdefType/enum.ts b/packages/markdown/src/doxygen/memberdefType/enum.ts index 1573238..068534a 100644 --- a/packages/markdown/src/doxygen/memberdefType/enum.ts +++ b/packages/markdown/src/doxygen/memberdefType/enum.ts @@ -8,6 +8,7 @@ import { } from '../../mappers'; import { xsdString } from '../../generic'; import { + codeBlock, joinParagraphs, languageCode, md, @@ -41,11 +42,12 @@ const template = ({ memberdefTitle(id, `${memberLabel(kind)} ${md(name)}`), memberdefBadges(context), location, - `\`\`\`${languageCode(language)} -enum ${name} { + codeBlock( + languageCode(language), + `enum ${name} { ${valuelist.map((e: string) => ` ${e}`).join(',\n')} -} -\`\`\``, +}` + ), memberdefDescription(context), memberdefReferences(context), ...enumvalue, diff --git a/packages/markdown/src/doxygen/memberdefType/event.ts b/packages/markdown/src/doxygen/memberdefType/event.ts index 13afebb..730a283 100644 --- a/packages/markdown/src/doxygen/memberdefType/event.ts +++ b/packages/markdown/src/doxygen/memberdefType/event.ts @@ -3,6 +3,7 @@ import { Element } from '@rgrove/parse-xml'; import { Mappers, applyToChildrenGrouped, $default } from '../../mappers'; import { xsdString } from '../../generic'; import { + codeBlock, joinParagraphs, languageCode, md, @@ -37,9 +38,7 @@ const template = ({ memberdefTitle(id, `${memberLabel(kind)} ${md(name)}`), memberdefBadges(context), location, - `\`\`\`${languageCode(language)} -${definition}${argsstring} -\`\`\``, + codeBlock(languageCode(language), `${definition}${argsstring}`), memberdefDescription(context), section('Type', type), memberdefReferences(context), diff --git a/packages/markdown/src/doxygen/memberdefType/function.ts b/packages/markdown/src/doxygen/memberdefType/function.ts index bd42b48..c50ab1e 100644 --- a/packages/markdown/src/doxygen/memberdefType/function.ts +++ b/packages/markdown/src/doxygen/memberdefType/function.ts @@ -3,6 +3,7 @@ import { Element } from '@rgrove/parse-xml'; import { Mappers, applyToChildrenGrouped, $default } from '../../mappers'; import { xsdString } from '../../generic'; import { + codeBlock, joinParagraphs, languageCode, md, @@ -40,10 +41,7 @@ const template = ({ joinParagraphs([ memberdefTitle(id, `${memberLabel(kind)} ${md(name)}`), memberdefBadges(context), - location, - `\`\`\`${languageCode(language)} -${definition}${argsstring} -\`\`\``, + codeBlock(languageCode(language), `${definition}${argsstring}`), memberdefDescription(context), sectionList('Parameters', param), section('Return type', type), diff --git a/packages/markdown/src/doxygen/memberdefType/property.ts b/packages/markdown/src/doxygen/memberdefType/property.ts index 369f900..c541a40 100644 --- a/packages/markdown/src/doxygen/memberdefType/property.ts +++ b/packages/markdown/src/doxygen/memberdefType/property.ts @@ -3,6 +3,7 @@ import { Element } from '@rgrove/parse-xml'; import { Mappers, applyToChildrenGrouped, $default } from '../../mappers'; import { xsdString } from '../../generic'; import { + codeBlock, joinParagraphs, languageCode, md, @@ -37,9 +38,7 @@ const template = ({ memberdefTitle(id, `${memberLabel(kind)} ${md(name)}`), memberdefBadges(context), location, - `\`\`\`${languageCode(language)} -${definition}${argsstring} -\`\`\``, + codeBlock(languageCode(language), `${definition}${argsstring}`), memberdefDescription(context), section('Return type', type), memberdefReferences(context), diff --git a/packages/markdown/src/doxygen/memberdefType/typedef.ts b/packages/markdown/src/doxygen/memberdefType/typedef.ts index 543289e..8125664 100644 --- a/packages/markdown/src/doxygen/memberdefType/typedef.ts +++ b/packages/markdown/src/doxygen/memberdefType/typedef.ts @@ -3,6 +3,7 @@ import { Element } from '@rgrove/parse-xml'; import { Mappers, applyToChildrenGrouped, $default } from '../../mappers'; import { xsdString } from '../../generic'; import { + codeBlock, joinParagraphs, languageCode, md, @@ -39,9 +40,7 @@ const template = ({ memberdefTitle(id, `${memberLabel(kind)} ${md(name)}`), memberdefBadges(context), location, - `\`\`\`${languageCode(language)} -${definition} -\`\`\``, + codeBlock(languageCode(language), `${definition}`), memberdefDescription(context), sectionList('Parameters', param), section('Return type', type), diff --git a/packages/markdown/src/doxygen/memberdefType/variable.ts b/packages/markdown/src/doxygen/memberdefType/variable.ts index 4517537..b8a2193 100644 --- a/packages/markdown/src/doxygen/memberdefType/variable.ts +++ b/packages/markdown/src/doxygen/memberdefType/variable.ts @@ -3,6 +3,7 @@ import { Element } from '@rgrove/parse-xml'; import { Mappers, applyToChildrenGrouped, $default } from '../../mappers'; import { xsdString } from '../../generic'; import { + codeBlock, joinParagraphs, languageCode, md, @@ -38,9 +39,10 @@ const template = ({ memberdefTitle(id, `${memberLabel(kind)} ${md(name)}`), memberdefBadges(context), location, - `\`\`\`${languageCode(language)} -${definition}${argsstring}${initializer ? ` ${initializer}` : ''} -\`\`\``, + codeBlock( + languageCode(language), + `${definition}${argsstring}${initializer ? ` ${initializer}` : ''}` + ), memberdefDescription(context), section('Type', type), memberdefReferences(context), diff --git a/packages/markdown/src/helpers/index.ts b/packages/markdown/src/helpers/index.ts index f382901..f058fc7 100644 --- a/packages/markdown/src/helpers/index.ts +++ b/packages/markdown/src/helpers/index.ts @@ -32,7 +32,7 @@ export const md = (text: string | string[]): any => : (text as string).replace(escapedMdChars, escapeMd); /** Helper for Markdown language code */ -export const languageCode = (code: string) => codes[code]; +export const languageCode = (code?: string) => (code ? codes[code] : undefined); /** Helper for links */ export const refLink = (refid: string, kindref: string) => { @@ -104,6 +104,12 @@ export const references = () => ) ); +/** Helper for code blocks */ +export const codeBlock = (language: string | undefined, code: string) => + `\`\`\`${language || ''} +${code} +\`\`\``; + /** Join non-empty strings */ export const joinStrings = (strings: any[], sep: string = '') => strings ? strings.filter((v) => !!v).join(sep) : '';