UNPKG

@yuebai008/cli

Version:

Command line interface for rapid qg-minigame development

1 lines 2.02 kB
import*as Marked from"../../third_party/marked/marked.js";export function resolveLazyDescription(e){const t=new Map;e.substitutions?.forEach(((e,r)=>{t.set(r,e())}));return{file:e.file,links:e.links.map((function(e){return{link:e.link,linkTitle:e.linkTitle()}})),substitutions:t}}export async function getFileContent(e){try{return(await fetch(e.toString())).text()}catch(t){throw new Error(`Markdown file ${e.toString()} not found. Make sure it is correctly listed in the relevant BUILD.gn files.`)}}export async function getMarkdownFileContent(e){return getFileContent(new URL(`descriptions/${e}`,import.meta.url))}export async function createIssueDescriptionFromMarkdown(e){return createIssueDescriptionFromRawMarkdown(substitutePlaceholders(await getMarkdownFileContent(e.file),e.substitutions),e)}export function createIssueDescriptionFromRawMarkdown(e,t){const r=Marked.Marked.lexer(e),n=findTitleFromMarkdownAst(r);if(!n)throw new Error("Markdown issue descriptions must start with a heading");return{title:n,markdown:r.slice(1),links:t.links}}const validPlaceholderMatchPattern=/\{(PLACEHOLDER_[a-zA-Z][a-zA-Z0-9]*)\}/g,validPlaceholderNamePattern=/PLACEHOLDER_[a-zA-Z][a-zA-Z0-9]*/;export function substitutePlaceholders(e,t){const r=new Set(t?t.keys():[]);validatePlaceholders(r);const n=e.replace(validPlaceholderMatchPattern,((e,n)=>{const i=t?t.get(n):void 0;if(!i)throw new Error(`No replacment provided for placeholder '${n}'.`);return r.delete(n),i}));if(r.size>0)throw new Error(`Unused replacements provided: ${[...r]}`);return n}function validatePlaceholders(e){const t=[...e].filter((e=>!validPlaceholderNamePattern.test(e)));if(t.length>0)throw new Error(`Invalid placeholders provided in the substitutions map: ${t}`)}export function findTitleFromMarkdownAst(e){return 0===e.length||"heading"!==e[0].type||1!==e[0].depth?null:e[0].text}export async function getIssueTitleFromMarkdownDescription(e){const t=await getMarkdownFileContent(e.file);return findTitleFromMarkdownAst(Marked.Marked.lexer(t))}