SYNC: Merge pull request #4 from dbgate/feature/charts

This commit is contained in:
Jan Prochazka
2025-06-09 09:15:21 +02:00
committed by Diflow
parent 6f69205818
commit f03cffe3f8
22 changed files with 1687 additions and 122 deletions

View File

@@ -5,7 +5,10 @@ import _isNumber from 'lodash/isNumber';
import _isPlainObject from 'lodash/isPlainObject';
import _pad from 'lodash/pad';
import _cloneDeepWith from 'lodash/cloneDeepWith';
import _isEmpty from 'lodash/isEmpty';
import _omitBy from 'lodash/omitBy';
import { DataEditorTypesBehaviour } from 'dbgate-types';
import isPlainObject from 'lodash/isPlainObject';
export type EditorDataType =
| 'null'
@@ -633,3 +636,38 @@ export function parseNumberSafe(value) {
}
return parseFloat(value);
}
const frontMatterRe = /^--\ >>>[ \t]*\n(.*)\n-- <<<[ \t]*\n/s;
export function getSqlFrontMatter(text: string, yamlModule) {
const match = text.match(frontMatterRe);
if (!match) return null;
const yamlContentMapped = match[1].replace(/^--[ ]?/gm, '');
return yamlModule.load(yamlContentMapped);
}
export function removeSqlFrontMatter(text: string) {
return text.replace(frontMatterRe, '');
}
export function setSqlFrontMatter(text: string, data: { [key: string]: any }, yamlModule) {
const textClean = removeSqlFrontMatter(text);
if (!isPlainObject(data)) {
return textClean;
}
const dataClean = _omitBy(data, v => v === undefined);
if (_isEmpty(dataClean)) {
return textClean;
}
const yamlContent = yamlModule.dump(dataClean);
const yamlContentMapped = yamlContent
.trimRight()
.split('\n')
.map(line => '-- ' + line)
.join('\n');
const frontMatterContent = `-- >>>\n${yamlContentMapped}\n-- <<<\n`;
return frontMatterContent + textClean;
}