woodpecker/docs/plugins/woodpecker-plugins/src/markdown.ts
Anbraten 62d82765fd
Improve plugins index (#1200)
Co-authored-by: qwerty287 <80460567+qwerty287@users.noreply.github.com>
2022-09-25 19:04:47 +02:00

27 lines
808 B
TypeScript

import { marked } from 'marked';
import { parse as YAMLParse } from 'yaml';
const tokens = ['---', '---'];
const regexHeader = new RegExp('^' + tokens[0] + '([\\s|\\S]*?)' + tokens[1]);
const regexContent = new RegExp('^ *?\\' + tokens[0] + '[^]*?' + tokens[1] + '*');
export function getHeader<T = any>(data: string): T {
const header = getRawHeader(data);
return YAMLParse(header) as T;
}
export function getRawHeader(data: string): string {
const header = regexHeader.exec(data);
if (!header) {
throw new Error("Can't get the header");
}
return header[1];
}
export function getContent(data: string): string {
const content = data.replace(regexContent, '').replace(/<!--(.*?)-->/gm, '');
if (!content) {
throw new Error("Can't get the content");
}
return marked(content);
}