Skip to content

Commit cb8742c

Browse files
Enhancement/issue 125 meta from config (#303)
* Rfc/issue 115 build time data access (#269) * graphql server working * apollo client connect to apollo server * connected header example using lit apollo * todo * todos * query and client + server refactor * schema refactoring * clean up console logging * alias all @greenwood/cli/data module imports * avoid paramater destructuring * graphql example in the header * multiple schemas * internal data sources documentation * shelf refactor and children query integration * refactor out ApolloQuery * ability to intercept client.query calls * basic semi-working implementation * remove extra config from server context * have puppeteer wait for graphql requests before returning content * fix and add test cases for apollo * merged resolvers not actually working * multiple queries support * everything working * todos * TODO tracking * fix fallback apollo client fetch handling * full test suite * cache json test cases * stablize test due to inconsistent data results ordering * clean up deps * todo cleanup * remove forced client call in SSG mode for client * represent graph through the schema * updated data docs * typos and grammer * typos and community link fixes * hello and graph queries working together * config def and resolvers * flesh out config schema and query * remove theme file from config * delete hello query example * formatting * documentation * app template with config title from data * tests for custom page title * refactoring meta to app-template and data instead of scaffolding and added tests * update coverage config * fix spelling mistake
1 parent 537133a commit cb8742c

File tree

20 files changed

+53
-140
lines changed

20 files changed

+53
-140
lines changed

nyc.config.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ module.exports = {
66
'packages/cli/src/data/*.js',
77
'packages/cli/src/lib/*.js',
88
'packages/cli/src/lifecycles/*.js',
9-
'packages/cli/src/plugins/*.js',
109
'packages/cli/src/tasks/*.js',
1110
'packages/plugin-*/src/*.js'
1211
],
@@ -20,10 +19,10 @@ module.exports = {
2019

2120
checkCoverage: true,
2221

23-
statements: 80,
24-
branches: 70,
25-
functions: 85,
26-
lines: 80,
22+
statements: 85,
23+
branches: 75,
24+
functions: 90,
25+
lines: 85,
2726

2827
watermarks: {
2928
statements: [75, 85],

packages/cli/src/data/queries/config.gql

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ query {
99
rel,
1010
content,
1111
property,
12-
value
12+
value,
13+
href
1314
},
1415
publicPath,
1516
title,

packages/cli/src/data/schema/config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ const configTypeDefs = gql`
1616
value: String,
1717
content: String,
1818
rel: String,
19-
property: String
19+
property: String,
20+
href: String
2021
}
2122
2223
type Config {

packages/cli/src/lifecycles/context.js

-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ const path = require('path');
33
const defaultTemplatesDir = path.join(__dirname, '../templates/');
44
const scratchDir = path.join(process.cwd(), './.greenwood/');
55
const publicDir = path.join(process.cwd(), './public');
6-
const metaComponent = path.join(__dirname, '../plugins/meta');
76

87
module.exports = initContexts = async({ config }) => {
98

@@ -46,7 +45,6 @@ module.exports = initContexts = async({ config }) => {
4645
: path.join(defaultTemplatesDir, notFoundPageTemplate),
4746
indexPageTemplate,
4847
notFoundPageTemplate,
49-
metaComponent,
5048
assetDir: path.join(userHasWorkspace ? userWorkspace : defaultTemplatesDir, 'assets')
5149
};
5250

packages/cli/src/lifecycles/scaffold.js

+1-19
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,6 @@ const writePageComponentsFromTemplate = async (compilation) => {
2323
});
2424
};
2525

26-
const loadPageMeta = (file, result, { metaComponent }) => {
27-
const { title, meta, route } = file;
28-
const metadata = {
29-
title,
30-
meta,
31-
route
32-
};
33-
34-
result = result.replace(/METAIMPORT/, `import '${metaComponent}'`);
35-
result = result.replace(/METADATA/, `const metadata = ${JSON.stringify(metadata)}`);
36-
result = result.replace(/METAELEMENT/, '<eve-meta .attributes=\${metadata}></eve-meta>');
37-
38-
return result;
39-
};
40-
4126
const writePageComponentToFile = async (target, filename, result) => {
4227
return new Promise(async(resolve, reject) => {
4328
try {
@@ -70,10 +55,7 @@ const writePageComponentsFromTemplate = async (compilation) => {
7055
return new Promise(async(resolve, reject) => {
7156
try {
7257
// Create Standard Page Component from Markdown File
73-
let result = await createPageComponent(file, context);
74-
75-
// Add Meta Data based on config
76-
result = loadPageMeta(file, result, context);
58+
const result = await createPageComponent(file, context);
7759

7860
// Determine path to newly scaffolded component
7961
const filePath = getPageComponentPath(file, context);

packages/cli/src/plugins/meta.js

-63
This file was deleted.

packages/cli/src/templates/app-template.js

+41-2
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,55 @@ class AppComponent extends LitElement {
4141
: ` - ${currentPage.title}`;
4242
const fullTitle = `${config.title}${currentPageTitleSuffix}`;
4343

44-
this.setDocoumentTitle(fullTitle);
44+
this.setDocumentTitle(fullTitle);
45+
this.setMeta(config.meta, currentPage);
4546
}
4647

47-
setDocoumentTitle(title) {
48+
setDocumentTitle(title = '') {
4849
const head = document.head;
4950
const titleElement = head.getElementsByTagName('title')[0];
5051

5152
titleElement.innerHTML = title;
5253
}
5354

55+
setMeta(meta = [], currentPage = {}) {
56+
let header = document.head;
57+
58+
meta.forEach(metaItem => {
59+
const metaType = metaItem.rel // type of meta
60+
? 'rel'
61+
: metaItem.name
62+
? 'name'
63+
: 'property';
64+
const metaTypeValue = metaItem[metaType]; // value of the meta
65+
let meta = document.createElement('meta');
66+
67+
if (metaType === 'rel') {
68+
// change to a <link> tag instead
69+
meta = document.createElement('link');
70+
71+
meta.setAttribute('rel', metaTypeValue);
72+
meta.setAttribute('href', metaItem.href);
73+
} else {
74+
const metaContent = metaItem.property === 'og:url'
75+
? `${metaItem.content}${currentPage.link}`
76+
: metaItem.content;
77+
78+
meta.setAttribute(metaType, metaItem[metaType]);
79+
meta.setAttribute('content', metaContent);
80+
}
81+
82+
const oldmeta = header.querySelector(`[${metaType}="${metaTypeValue}"]`);
83+
84+
// rehydration
85+
if (oldmeta) {
86+
header.replaceChild(meta, oldmeta);
87+
} else {
88+
header.appendChild(meta);
89+
}
90+
});
91+
}
92+
5493
render() {
5594
return html`
5695
MYROUTES

packages/cli/src/templates/page-template.js

-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import { html, LitElement } from 'lit-element';
22
MDIMPORT;
3-
METAIMPORT;
4-
METADATA;
53

64
class PageTemplate extends LitElement {
75
render() {
86
return html`
9-
METAELEMENT
107
<div class='wrapper'>
118
<div class='page-template content'>
129
<entry></entry>

packages/cli/test/cases/build.config.theme/src/templates/page-template.js

-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
import { html, LitElement } from 'lit-element';
22
import '../styles/my-brand.css';
33
MDIMPORT;
4-
METAIMPORT;
5-
METADATA;
64

75
class PageTemplate extends LitElement {
86
render() {
97
return html`
10-
METAELEMENT
118
<div class='wrapper'>
129
<div class='page-template content owen-test'>
1310
<entry></entry>

packages/cli/test/cases/build.data.graph/src/templates/blog-template.js

-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import ChildrenQuery from '@greenwood/cli/data/queries/children';
44
import '../components/header';
55

66
MDIMPORT;
7-
METAIMPORT;
8-
METADATA;
97

108
class BlogTemplate extends LitElement {
119

@@ -39,7 +37,6 @@ class BlogTemplate extends LitElement {
3937
const { posts } = this;
4038

4139
return html`
42-
METAELEMENT
4340
4441
<div class='container'>
4542
<app-header></app-header>

packages/cli/test/cases/build.data.graph/src/templates/page-template.js

-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import { html, LitElement } from 'lit-element';
22
import '../components/header';
33

44
MDIMPORT;
5-
METAIMPORT;
6-
METADATA;
75

86
class PageTemplate extends LitElement {
97

packages/cli/test/cases/build.default.workspace-getting-started/src/templates/blog-template.js

-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import '../components/footer';
33
import '../components/header';
44

55
MDIMPORT;
6-
METAIMPORT;
7-
METADATA;
86

97
class BlogTemplate extends LitElement {
108

@@ -17,8 +15,6 @@ class BlogTemplate extends LitElement {
1715
<style>
1816
1917
</style>
20-
21-
METAELEMENT
2218
2319
<div class='container'>
2420
<app-header></app-header>

packages/cli/test/cases/build.default.workspace-template-page-style/src/templates/page-template.js

-3
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,13 @@ import { html, LitElement } from 'lit-element';
22
import '../styles/theme.css';
33
import css from '../styles/style.css';
44
MDIMPORT;
5-
METAIMPORT;
6-
METADATA;
75

86
class PageTemplate extends LitElement {
97
render() {
108
return html`
119
<style>
1210
${css}
1311
</style>
14-
METAELEMENT
1512
<div class='wrapper'>
1613
<div class='page-template content owen-test'>
1714
<entry></entry>

packages/cli/test/cases/build.default.workspace-template-page/src/templates/page-template.js

-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import { html, LitElement } from 'lit-element';
22
MDIMPORT;
3-
METAIMPORT;
4-
METADATA;
53

64
class PageTemplate extends LitElement {
75
render() {
86
return html`
9-
METAELEMENT
107
<div class='wrapper'>
118
<div class='page-template blog-content content owen-test'>
129
<entry></entry>

packages/cli/test/cases/build.default.workspace-user-directory-mapping/src/templates/page-template.js

-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
import { html, LitElement } from 'lit-element';
22
import '../components/header/header';
3-
43
MDIMPORT;
5-
METAIMPORT;
6-
METADATA;
74

85
class PageTemplate extends LitElement {
96
render() {
107
return html`
11-
METAELEMENT
128
<div class='wrapper'>
139
<div class='page-template blog-content content owen-test'>
1410
<entry></entry>

www/pages/docs/data.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,8 @@ query {
248248
rel,
249249
content,
250250
property,
251-
value
251+
value,
252+
href
252253
},
253254
publicPath,
254255
title,

www/pages/docs/layouts.md

-10
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,10 @@ Here is an example `page-template.js` (the [default](https://github.com/ProjectE
1212
```render js
1313
import { html, LitElement } from 'lit-element';
1414
MDIMPORT;
15-
METAIMPORT;
16-
METADATA;
1715
1816
class PageTemplate extends LitElement {
1917
render() {
2018
return html\`
21-
METAELEMENT
2219
<div class='wrapper'>
2320
<div class='page-template content'>
2421
<entry></entry>
@@ -43,13 +40,6 @@ MDIMPORT;
4340
`MDIMPORT;` Tells Greenwood to import a markdown file into this component. But we also have to define where the compiled markdown element(page) will be placed within our page-template. Hence below within our `render()` method you will see the `<entry></entry>` element. That defines exactly where to place it.
4441

4542

46-
```render js
47-
METAIMPORT;
48-
METADATA;
49-
```
50-
51-
`METAIMPORT;` and `METADATA;` hook variables import the default greenwood meta component and data which handles all your configured meta data. You can then render this component within the `render()` method using the `METAELEMENT` variable hook.
52-
5343
The complete example can be found in the [greenwood source](https://github.com/ProjectEvergreen/greenwood/blob/master/packages/cli/templates/page-template.js) which is the default page-template.js if no other is defined.
5444

5545
With a completed page-template.js present in your `src/templates/` folder you can define which page uses it via front-matter at the top of any markdown file. See [Front Matter Docs](/docs/front-matter#define-template) for more information. Simply including a file named `page-template.js` will overwrite the greenwood default template for all markdown files, without needing to declare the template at the top of markdown file.

www/pages/getting-started/key-concepts.md

+1-4
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,10 @@ For reference, here's what the default page template included by Greenwood looks
4242
```render javascript
4343
import { html, LitElement } from 'lit-element';
4444
MDIMPORT;
45-
METAIMPORT;
46-
METADATA;
4745
4846
class PageTemplate extends LitElement {
4947
render() {
5048
return html\`
51-
METAELEMENT
5249
<div class='wrapper'>
5350
<div class='page-template content'>
5451
<entry></entry>
@@ -61,7 +58,7 @@ class PageTemplate extends LitElement {
6158
customElements.define('page-template', PageTemplate);
6259
```
6360

64-
> Don't worry too much about the capitalized expressions, this is discussed in more detail in our [docs](/docs/template/). Also, as seen here, Greenwood provides a version of [**LitElement**](https://lit-element.polymer-project.org/) by default that you can use for your own components if you would like.
61+
> Don't worry too much about the capitalized expression, this is discussed in more detail in our [docs](/docs/template/). Also, as seen here, Greenwood provides a version of [**LitElement**](https://lit-element.polymer-project.org/) by default that you can use for your own components if you would like.
6562
6663

6764
### Pages

0 commit comments

Comments
 (0)