1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-11-24 08:12:24 +02:00

Tools: Improve error message when website building fails

This commit is contained in:
Laurent Cozic 2022-11-15 16:00:06 +00:00
parent 341c9029c4
commit 0cfa07b1b0
3 changed files with 43 additions and 28 deletions

View File

@ -144,12 +144,17 @@ function renderPageToHtml(md: string, targetPath: string, templateParams: Templa
} }
function renderFileToHtml(sourcePath: string, targetPath: string, templateParams: TemplateParams) { function renderFileToHtml(sourcePath: string, targetPath: string, templateParams: TemplateParams) {
let md = readFileSync(sourcePath, 'utf8'); try {
if (templateParams.isNews) { let md = readFileSync(sourcePath, 'utf8');
md = processNewsMarkdown(md, sourcePath); if (templateParams.isNews) {
md = processNewsMarkdown(md, sourcePath);
}
md = stripOffFrontMatter(md).doc;
return renderPageToHtml(md, targetPath, templateParams);
} catch (error) {
error.message = `Could not render file: ${sourcePath}: ${error.message}`;
throw error;
} }
md = stripOffFrontMatter(md).doc;
return renderPageToHtml(md, targetPath, templateParams);
} }
function makeHomePageMd() { function makeHomePageMd() {

View File

@ -73,18 +73,23 @@ const getPosts = async (newsDir: string): Promise<Post[]> => {
}; };
const getPostContent = async (post: Post): Promise<PostContent> => { const getPostContent = async (post: Post): Promise<PostContent> => {
const raw = await readFile(post.path, 'utf8'); try {
const parsed = stripOffFrontMatter(raw); const raw = await readFile(post.path, 'utf8');
const lines = parsed.doc.split('\n'); const parsed = stripOffFrontMatter(raw);
const titleLine = lines[0]; const lines = parsed.doc.split('\n');
if (!titleLine.startsWith('# ')) throw new Error('Cannot extract title from post: no header detected'); const titleLine = lines[0];
lines.splice(0, 1); if (!titleLine.startsWith('# ')) throw new Error('Cannot extract title from post: no header detected');
lines.splice(0, 1);
return { return {
title: titleLine.substr(1).trim(), title: titleLine.substr(1).trim(),
body: lines.join('\n').trim(), body: lines.join('\n').trim(),
parsed, parsed,
}; };
} catch (error) {
error.message = `Could not get post content: ${post.id}: ${post.path}: ${error.message}`;
throw error;
}
}; };
const execApi = async (method: HttpMethod, path: string, body: Record<string, string | number> = null) => { const execApi = async (method: HttpMethod, path: string, body: Record<string, string | number> = null) => {

View File

@ -7,19 +7,24 @@ interface ReadmeDoc {
} }
export async function readmeFileTitleAndBody(sourcePath: string): Promise<ReadmeDoc> { export async function readmeFileTitleAndBody(sourcePath: string): Promise<ReadmeDoc> {
let md = await readFile(sourcePath, 'utf8'); try {
md = stripOffFrontMatter(md).doc.trim(); let md = await readFile(sourcePath, 'utf8');
const r = md.match(/^# (.*)/); md = stripOffFrontMatter(md).doc.trim();
const r = md.match(/^# (.*)/);
if (!r) { if (!r) {
throw new Error(`Could not determine title for Markdown file: ${sourcePath}`); throw new Error('Could not determine title');
} else { } else {
const lines = md.split('\n'); const lines = md.split('\n');
lines.splice(0, 1); lines.splice(0, 1);
return { return {
title: r[1], title: r[1],
body: lines.join('\n'), body: lines.join('\n'),
}; };
}
} catch (error) {
error.message = `On ${sourcePath}: ${error.message}`;
throw error;
} }
} }