core: support ContentNode[] format

This commit is contained in:
undefined committed 2020-12-05 23:44:17 +08:00
1 parent 151cc79f36
commit b0aa983e6f
12 files changed
+640 -130

No files matched your search

+1 -45
View File
@@ -4,6 +4,7 @@ import cluster from 'cluster';
import path from 'path';
import serialize from 'serialize-javascript';
import Lru from 'lru-cache';
import type { ContentNode } from './interface';
declare global {
interface StringConstructor {
@@ -224,48 +225,3 @@ function deepen(modifyString: (source: string) => string) {
export const camelCase = deepen((source) => source.replace(/[_-][a-z]/g, (str) => str.slice(1).toUpperCase()));
export const paramCase = deepen((source) => source.replace(/_/g, '-').replace(/(?<!^)[A-Z]/g, (str) => `-${str.toLowerCase()}`));
export const snakeCase = deepen((source) => source.replace(/-/g, '_').replace(/(?<!^)[A-Z]/g, (str) => `_${str.toLowerCase()}`));
interface ProblemSource {
description?: string,
input?: string,
output?: string,
samples?: [string, string][],
hint?: string,
source?: string,
}
export function buildContent(source: ProblemSource, type: 'markdown' | 'html' = 'markdown', translate?: Function) {
const _ = translate || ((s: string) => s);
return type === 'html'
? [
...source.description ? [`<h2>${_('Description')}</h2>`, source.description] : [],
...source.input ? [`<h2>${_('Input Format')}</h2>`, source.input] : [],
...source.output ? [`<h2>'${_('Output Format')}</h2>`, source.output] : [],
...(source.samples).map((sample, i) => [
`<h2>${_('Sample Input')} ${i + 1}</h2><pre>`,
sample[0],
`</pre><h2>${_('Sample Output')} ${i + 1}</h2><pre>`,
sample[1],
'</pre>',
].join('\n')),
...source.hint ? [`<h2>${_('Hint')}</h2>`, source.hint] : [],
...source.source ? [`<h2>${_('Source')}</h2>`, source.source] : [],
].join('\n')
: [
...source.description ? [`## ${_('Description')}`, source.description] : [],
...source.input ? [`## ${_('Input Format')}`, source.input] : [],
...source.output ? [`## '${_('Output Format')}`, source.output] : [],
...(source.samples).map((sample, i) => [
`## ${_('Sample Input')} ${i + 1}`,
'```',
sample[0],
'```',
`## ${_('Sample Output')} ${i + 1}`,
'```',
sample[1],
'```',
].join('\n')),
...source.hint ? [`## ${_('Hint')}`, source.hint] : [],
...source.source ? [`## ${_('Source')}`, source.source] : [],
].join('\n');
}