Skip to content

Commit

Permalink
feat(clayui.com): adds implementation to render pages consuming from …
Browse files Browse the repository at this point in the history
…Liferay
  • Loading branch information
matuzalemsteles committed Feb 23, 2024
1 parent 624f173 commit d3d6020
Show file tree
Hide file tree
Showing 9 changed files with 286 additions and 21 deletions.
5 changes: 4 additions & 1 deletion clayui.com/.env.development
@@ -1 +1,4 @@
GATSBY_CLAY_NIGHTLY=true
GATSBY_CLAY_NIGHTLY=true
LIFERAY_HOST=http://127.0.0.1:8080
LIFERAY_USERNAME=
LIFERAY_SIDE_ID=
8 changes: 8 additions & 0 deletions clayui.com/gatsby-config.js
Expand Up @@ -56,6 +56,14 @@ module.exports = {
},
resolve: 'gatsby-source-filesystem',
},
{
options: {
host: process.env.LIFERAY_HOST,
siteId: process.env.LIFERAY_SIDE_ID,
username: process.env.LIFERAY_USERNAME,
},
resolve: 'gatsby-source-liferay',
},
{
options: {
extensions: ['.mdx'],
Expand Down
118 changes: 101 additions & 17 deletions clayui.com/gatsby/createPages.js
Expand Up @@ -20,19 +20,32 @@ const TAB_MAP_NAME = {
markup: 'Markup',
};

const getFileName = (path) => {
const uri = path.split('/');

return uri[uri.length - 1].replace('.html', '');
};

const slugWithBar = (path) => {
return path.startsWith('/') ? path : `/${path}`;
};

const getTabs = (permalink, pathGroup) => {
const sortTabs = (a) => (a.name === 'API' ? 1 : -1);
const fileSlug = getFileName(permalink);

const tabs = pathGroup.filter(
({
node: {
fields: {mainTabURL},
fields: {mainTabURL, slug, type},
},
}) => mainTabURL === permalink
}) => {
if (type === 'LiferayDocument') {
return slug.substring(1) === fileSlug;
}

return mainTabURL === permalink;
}
);

if (tabs.length === 0) {
Expand All @@ -48,12 +61,24 @@ const getTabs = (permalink, pathGroup) => {
.map(
({
node: {
fields: {slug},
fields: {slug, type},
},
}) => ({
href: slug,
name: TAB_MAP_NAME[path.basename(slug, '.html')],
})
}) => {
if (type === 'LiferayDocument') {
return {
href: `${permalink.replace(
'.html',
''
)}/design.html`,
name: 'Design',
};
}

return {
href: slug,
name: TAB_MAP_NAME[path.basename(slug, '.html')],
};
}
)
.sort(sortTabs),
];
Expand Down Expand Up @@ -122,19 +147,57 @@ const createDocs = (actions, edges, mdx, pathGroup) => {
(slug.includes('docs/') || slug.includes('blog/')) &&
layout !== 'redirect'
) {
const tabs = getTabs(mainTabURL || slug, pathGroup);
const fileSlug = getFileName(mainTabURL || slug);

const pagePathGroup = pathGroup
.filter(
({
node: {
fields: {type},
},
}) => type !== 'LiferayDocument'
)
.map(
({
node: {
fields: {slug},
},
}) => slug
);

const designPageData = pathGroup.find(
(item) =>
item.node.fields.slug.substring(1) === fileSlug
);
const designPage = tabs.find(
(item) => item.name === 'Design'
);

if (designPage) {
createPage({
component: template,
context: {
mainTabURL,
pageRemote: {
html: designPageData.node.fields.html,
title: designPageData.node.fields.title,
},
pathGroup: pagePathGroup,
slug: designPage.href,
tabs,
},
path: designPage.href,
});
}

createPage({
component: template,
context: {
mainTabURL,
pathGroup: pathGroup.map(
({
node: {
fields: {slug},
},
}) => slug
),
pathGroup: pagePathGroup,
slug,
tabs: getTabs(mainTabURL || slug, pathGroup),
tabs,
},
path: slug,
});
Expand Down Expand Up @@ -187,6 +250,17 @@ module.exports = async ({actions, graphql}) => {
}
}
}
allLiferayDocument {
edges {
node {
type
id
title
html
slug
}
}
}
}
`).then(async ({data, errors}) => {
if (errors) {
Expand All @@ -204,11 +278,21 @@ module.exports = async ({actions, graphql}) => {
}) => mainTabURL
);

const allDocuments = data.allLiferayDocument.edges.map(({node}) => ({
node: {fields: node},
}));

if (data.allMdx.edges) {
createDocs(actions, data.allMdx.edges, true, pathGroup);
createDocs(actions, data.allMdx.edges, true, [
...pathGroup,
...allDocuments,
]);
}

createDocs(actions, data.allMarkdownRemark.edges, false, pathGroup);
createDocs(actions, data.allMarkdownRemark.edges, false, [
...pathGroup,
...allDocuments,
]);

const newestBlogEntry = await graphql(
`
Expand Down
1 change: 1 addition & 0 deletions clayui.com/package.json
Expand Up @@ -49,6 +49,7 @@
"classnames": "^2.2.6",
"clipboard": "^2.0.4",
"dotenv": "^6.0.0",
"fetch-undici": "3.x",
"gatsby": "^3.14.1",
"gatsby-plugin-google-gtag": "^4.3.0",
"gatsby-plugin-mdx": "^2.14.0",
Expand Down
137 changes: 137 additions & 0 deletions clayui.com/plugins/gatsby-source-liferay/gatsby-node.js
@@ -0,0 +1,137 @@
/**
* SPDX-FileCopyrightText: © 2024 Liferay, Inc. <https://liferay.com>
* SPDX-License-Identifier: BSD-3-Clause
*/

const {Headers, fetch} = require('fetch-undici');

const fetchLiferay = async (slug, siteId, username, host) => {
return await fetch(
new URL(
`/o/headless-delivery/v1.0/sites/${siteId}/site-pages${slug}`,
host
),
{
headers: new Headers({
Authorization: `Basic ${btoa(username)}`,
'User-Agent': 'Clay',
'content-type': 'application/json',
}),
}
)
.then(async (response) => {
const {status} = response;

const responseContentType = response.headers.get('content-type');

if (status === 204) {
return status;
} else if (
response.ok &&
responseContentType === 'application/json'
) {
return response.json();
} else {
const data = await response.text();

if (data.includes('NOT_FOUND')) {
return JSON.parse(data);
}

return {data};
}
})
// eslint-disable-next-line no-console
.catch((error) => console.log(error));
};

const convertTimestamps = (nextObj, prevObj, prevKey) => {
if (typeof nextObj === `object`) {
Object.keys(nextObj).map((key) =>
convertTimestamps(nextObj[key], nextObj, key)
);
} else {
if (typeof nextObj === `number` && nextObj >> 0 !== nextObj) {
const date = new Date(nextObj);
if (date.getTime() === nextObj) {
prevObj[prevKey] = date.toISOString().slice(0, 10);
}
}
}
};

exports.sourceNodes = async (
{actions, createContentDigest, createNodeId},
{host, siteId, username}
) => {
const {createNode} = actions;

try {
const {items} = await fetchLiferay('', siteId, username, host);

const resources = await Promise.all(
items
.filter(
(resource) => !['Home', 'Search'].includes(resource.title)
)
.map(async (resource) => {
const {data} = await fetchLiferay(
`${resource.friendlyUrlPath}/rendered-page`,
siteId,
username,
host
);

return {
dateCreated: resource.dateCreated,
dateModified: resource.dateModified,
datePublished: resource.datePublished,
html: data,
id: createNodeId(resource.uuid),
liferay_id: resource.uuid,
slug: resource.friendlyUrlPath,
title: resource.title,
type: 'LiferayDocument',
};
})
);

resources.map((resource) => {
convertTimestamps(resource);

const contentDigest = createContentDigest(resource);

const node = {
...resource,
children: [],
internal: {
contentDigest,
type: `LiferayDocument`,
},
parent: null,
};

createNode(node);
});
} catch (error) {
console.error(error);
process.exit(1);
}
};

exports.createSchemaCustomization = async ({actions}) => {
const typeDefs = `
type LiferayDocument implements Node {
title: String
slug: String
html: String
dateCreated: Date @dateformat
dateModified: Date @dateformat
datePublished: Date @dateformat
liferay_id: String
type: String
}
`;

actions.createTypes(typeDefs);
};
4 changes: 4 additions & 0 deletions clayui.com/plugins/gatsby-source-liferay/package.json
@@ -0,0 +1,4 @@
{
"name": "gatsby-source-liferay",
"version": "0.0.1"
}
1 change: 1 addition & 0 deletions clayui.com/src/styles/_guide.scss
Expand Up @@ -87,6 +87,7 @@

th {
padding-left: 0;
vertical-align: middle;
}
}

Expand Down
7 changes: 5 additions & 2 deletions clayui.com/src/templates/docs.js
Expand Up @@ -165,7 +165,7 @@ export default function Documentation(props) {
const {
data,
location,
pageContext: {tabs = [], slug},
pageContext: {tabs = [], slug, pageRemote},
} = props;

const {allMarkdownRemark, allMdx, mainTab, pageMd, pageMdx} = data;
Expand Down Expand Up @@ -351,7 +351,10 @@ export default function Documentation(props) {

<CodeClipboard>
<Content
html={html}
html={
html ||
pageRemote.html
}
jsx={body}
/>
</CodeClipboard>
Expand Down

0 comments on commit d3d6020

Please sign in to comment.