Skip to content

Commit

Permalink
Add helper for code block
Browse files Browse the repository at this point in the history
  • Loading branch information
fredericbonnet committed Nov 12, 2020
1 parent 00270ec commit 3e9ec74
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 59 deletions.
12 changes: 4 additions & 8 deletions packages/markdown/src/doxygen/graphType.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@ describe('graphType', () => {

specify('empty', () => {
const xml = `<incdepgraph></incdepgraph>`;
const md = `
\`\`\`mermaid
const md = `\`\`\`mermaid
graph LR
\`\`\`
`;
\`\`\``;
expect(render('LR', false)(xml)).to.equal(md);
});
specify('full example', () => {
Expand All @@ -38,8 +36,7 @@ graph LR
<childnode refid="3" relation="include"></childnode>
</node>
</incdepgraph>`;
const md = `
\`\`\`mermaid
const md = `\`\`\`mermaid
graph LR
1["first node"]
click 1 "file_12345.md#file_12345"
Expand All @@ -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);
});
});
8 changes: 2 additions & 6 deletions packages/markdown/src/doxygen/graphType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => ({
Expand All @@ -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;

Expand Down
18 changes: 6 additions & 12 deletions packages/markdown/src/doxygen/listingType.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ describe('listingType', () => {

specify('empty', () => {
const xml = `<programlisting></programlisting>`;
const md = `
\`\`\`
const md = `\`\`\`
\`\`\`
`;
\`\`\``;
expect(render(xml)).to.equal(md);
});

Expand All @@ -29,12 +27,10 @@ describe('listingType', () => {
<codeline><highlight class="normal">Basic</highlight></codeline>
<codeline><highlight class="normal">Normal<sp/></highlight><highlight class="comment">/*<sp/>Comment<sp/>*/</highlight></codeline>
</programlisting>`;
const md = `
\`\`\`
const md = `\`\`\`
Basic
Normal /* Comment */
\`\`\`
`;
\`\`\``;
expect(render(xml)).to.equal(md);
});

Expand All @@ -50,12 +46,10 @@ Normal /* Comment */
<codeline><highlight class="normal">Basic</highlight></codeline>
<codeline><highlight class="normal">Normal<sp/></highlight><highlight class="comment">/*<sp/>Comment<sp/>*/</highlight></codeline>
</programlisting>`;
const md = `
\`\`\`cpp
const md = `\`\`\`cpp
Basic
Normal /* Comment */
\`\`\`
`;
\`\`\``;
expect(render(xml)).to.equal(md);
});
});
Expand Down
8 changes: 2 additions & 6 deletions packages/markdown/src/doxygen/listingType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => ({
Expand All @@ -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'));
};
13 changes: 7 additions & 6 deletions packages/markdown/src/doxygen/memberdefType/define.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down
10 changes: 6 additions & 4 deletions packages/markdown/src/doxygen/memberdefType/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from '../../mappers';
import { xsdString } from '../../generic';
import {
codeBlock,
joinParagraphs,
languageCode,
md,
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 2 additions & 3 deletions packages/markdown/src/doxygen/memberdefType/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down
6 changes: 2 additions & 4 deletions packages/markdown/src/doxygen/memberdefType/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down
5 changes: 2 additions & 3 deletions packages/markdown/src/doxygen/memberdefType/property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down
5 changes: 2 additions & 3 deletions packages/markdown/src/doxygen/memberdefType/typedef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down
8 changes: 5 additions & 3 deletions packages/markdown/src/doxygen/memberdefType/variable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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),
Expand Down
8 changes: 7 additions & 1 deletion packages/markdown/src/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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) : '';
Expand Down

0 comments on commit 3e9ec74

Please sign in to comment.