Skip to content

Commit 536d11c

Browse files
authored
feat(scully): better handiling of missing <scully-content> (#462)
1 parent 53bd8ec commit 536d11c

4 files changed

Lines changed: 38 additions & 12 deletions

File tree

.vscode/settings.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
"statusBar.foreground": "#e7e7e7",
2323
"panel.border": "#1b2c8f",
2424
"sideBar.border": "#1b2c8f",
25-
"editorGroup.border": "#1b2c8f"
25+
"editorGroup.border": "#1b2c8f",
26+
"statusBar.border": "#131f64",
27+
"titleBar.border": "#131f64"
2628
},
2729
"files.exclude": {
2830
"**/scully/bin/**/*": true

scully/renderPlugins/content-render-utils/insertContent.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ export function insertContent(startTag: string, endTag: string, html: string, in
55
const [takeout, endText] = rest.split(endTag);
66
return [openingText, startTag, insertText, endTag, ...extras, endText].join('');
77
} catch (e) {}
8-
logWarn(`missing "${yellow('<scully-content>')}" or "${yellow('httpClientModule')}"`);
8+
/** warning is already handled, only put in stub content. */
9+
// logWarn(`missing "${yellow('<scully-content>')}"`);
910
return `<h1>Scully could not find the &lt.scully-content&gt. tag in this page.</h1>
1011
<p>This error can happen when you forgot to put the mandatory "scully-content" in the component that is rendering this page?</p>
11-
<p>It may also occur if the 'httpClientModule' is not load in your app.module</p>
12+
<p>Or when the tag is not shown on page load. Did you put it inside an \`*ngIf\`?</p>
1213
`;
1314
}

scully/renderPlugins/content-render-utils/readFileAndCheckPrePublishSlug.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {readFileSync, writeFileSync} from 'fs';
22
import {stringify} from 'yamljs';
33
import {randomString} from '../../utils/randomString';
4+
import {logWarn, yellow} from '../../utils';
45
const fm = require('front-matter');
56

67
export interface ContentMetaData {
@@ -19,6 +20,9 @@ export async function readFileAndCheckPrePublishSlug(file) {
1920
const originalFile = readFileSync(file, 'utf-8');
2021
const {attributes: meta, body: fileContent}: {attributes: ContentMetaData; body: string} = fm(originalFile);
2122
let prePublished = false;
23+
if (fileContent.trim() === '') {
24+
logWarn(`Content file "${yellow(file)}" has no content!`);
25+
}
2226
if (meta.hasOwnProperty('published') && meta.published === false) {
2327
/** this post needs an pre-publish slug */
2428
const slugs = Array.isArray(meta.slugs) ? meta.slugs : [];

scully/renderPlugins/contentRenderPlugin.ts

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {insertContent} from './content-render-utils/insertContent';
77
import {readFileAndCheckPrePublishSlug} from './content-render-utils/readFileAndCheckPrePublishSlug';
88
import {JSDOM} from 'jsdom';
99
import {customMarkdownOptions} from './customMarkdownOptions';
10+
import {ssl} from '../utils';
1011

1112
registerPlugin('render', 'contentFolder', contentRenderPlugin);
1213

@@ -18,18 +19,36 @@ export async function contentRenderPlugin(html: string, route: HandledRoute) {
1819
try {
1920
const extension = file.split('.').pop();
2021
const {fileContent} = await readFileAndCheckPrePublishSlug(file);
21-
// TODO: create additional "routes" for every slug
22-
const attr = getIdAttrName(
23-
html
24-
.split('<scully-content')[1]
25-
.split('>')[0]
26-
.trim()
27-
);
28-
const additionalHTML = await customMarkdownOptions(await handleFile(extension, fileContent));
22+
let attr = '';
23+
try {
24+
attr = getIdAttrName(
25+
html
26+
.split('<scully-content')[1]
27+
.split('>')[0]
28+
.trim()
29+
);
30+
} catch (e) {
31+
logWarn(`
32+
----------------
33+
Error, missing "${yellow('<scully-content>')}" in route "${yellow(route.route)}"
34+
without <scully-content> we can not render this route.
35+
Make sure it is in there, and not inside any conditionals (*ngIf)
36+
You can check this by opening "${yellow(`http${ssl ? 'S' : ''}://localhost:4200/${route.route}`)}"
37+
when you serve your app with ${yellow('ng serve')} and then in the browsers console run:
38+
${yellow(`document.querySelector('scully-content')`)}
39+
----------------
40+
`);
41+
}
42+
let additionalHTML = '';
43+
try {
44+
additionalHTML = await customMarkdownOptions(await handleFile(extension, fileContent));
45+
} catch (e) {
46+
logWarn(`Error, while reading content for "${yellow(route.route)}" from file: "${yellow(file)}"`);
47+
}
2948
const htmlWithNgAttr = addNgIdAttribute(additionalHTML, attr);
3049
return insertContent(scullyBegin, scullyEnd, html, htmlWithNgAttr, getScript(attr));
3150
} catch (e) {
32-
logWarn(`Error, probably missing "${yellow('<scully-content>')}" for ${yellow(file)}`);
51+
logWarn(`Error, while rendering content for "${yellow(route.route)}" from file: "${yellow(file)}"`);
3352
console.error(e);
3453
}
3554
}

0 commit comments

Comments
 (0)