Skip to content

Commit 241789a

Browse files
committed
Make the site generation script hosting agnostic
1 parent e7910c6 commit 241789a

File tree

6 files changed

+25
-28
lines changed

6 files changed

+25
-28
lines changed

src/lib/markdown.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ const probe = require("probe-image-size")
44
export async function renderMarkdown(markdown: string): Promise<string> {
55
const converter = new showdown.Converter({ metadata: true, tables: true })
66
let html = converter.makeHtml(markdown)
7-
const imgTags = html.match(/<img [^>]*src="[^"]*"[^>]*>/gm)
8-
if (imgTags !== null) {
7+
const imgTags = html?.match(/<img [^>]*src="[^"]*"[^>]*>/gm)
8+
if (imgTags) {
99
for (let img of imgTags) {
1010
if (img.includes('width="')) {
1111
// Skip images that already have their width/height set

src/pages/[name].tsx

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,12 @@ export default function Doc(
6969

7070
export async function getStaticPaths() {
7171
// Connect and configure the TerminusClient
72-
const client = new TerminusClient.WOQLClient(
73-
"https://cloud.terminusdb.com/TerminatorsX",
74-
{
75-
76-
organization: "TerminatorsX",
77-
db: "terminusCMS_docs",
78-
token: process.env.TERMINUSDB_API_TOKEN,
79-
}
80-
)
72+
const client = new TerminusClient.WOQLClient(process.env.TERMINUSDB_API_ENDPOINT, {
73+
user: process.env.TERMINUSDB_USER,
74+
organization: process.env.TERMINUSDB_TEAM,
75+
db: process.env.TERMINUSDB_DB,
76+
token: process.env.TERMINUSDB_API_TOKEN,
77+
})
8178
const docs = await client.getDocument({ "@type": "Page", as_list: true })
8279
const exceptions = ["python", "openapi", "javascript"]
8380
const paths = docs
@@ -93,10 +90,10 @@ export async function getStaticProps({ params }) {
9390
headers: { Authorization: `Token ${process.env.TERMINUSDB_API_TOKEN}` },
9491
}
9592
const doc = await axios.post(
96-
"https://cloud.terminusdb.com/TerminatorsX/api/graphql/TerminatorsX/terminusCMS_docs",
93+
`${process.env.TERMINUSDB_API_ENDPOINT}/api/graphql/${process.env.TERMINUSDB_TEAM}/${process.env.TERMINUSDB_DB}`,
9794
{
9895
query: `query {
99-
Page(filter: {slug: {eq: "${params['name']}"}}) {
96+
Page(filter: {slug: {eq: "${params["name"]}"}}) {
10097
slug,
10198
title {
10299
value
@@ -117,7 +114,7 @@ export async function getStaticProps({ params }) {
117114
const docResult = doc.data.data.Page[0]
118115
let html = ""
119116
if (docResult["body"] !== null) {
120-
html = await renderMarkdown(docResult["body"]["value"])
117+
html = await renderMarkdown(docResult["body"]?.["value"]??"")
121118
}
122119
const entry = { html: html, document: docResult }
123120
return { props: { entry, menu } }

src/pages/index.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,21 @@ export default function Home(props: { menu: any[]; entry: any[] }) {
1818
menu={props.menu}
1919
entry={props.entry}
2020
displayElement={displayElement}
21-
heading={props.entry.document.title.value}
21+
heading={props.entry.document?.title?.value ?? ""}
2222
subtitle={getSubTitle(props.entry.document)}
23-
seo_metadata={props.entry.document.seo_metadata}
23+
seo_metadata={props.entry.document?.seo_metadata}
2424
/>
2525
)
2626
}
2727

2828
export async function getStaticProps({ params }) {
2929
// Connect and configure the TerminusClient
3030
const client = new TerminusClient.WOQLClient(
31-
"https://cloud.terminusdb.com/TerminatorsX",
31+
process.env.TERMINUSDB_API_ENDPOINT,
3232
{
33-
34-
organization: "TerminatorsX",
35-
db: "terminusCMS_docs",
33+
user: process.env.TERMINUSDB_USER,
34+
organization: process.env.TERMINUSDB_TEAM,
35+
db: process.env.TERMINUSDB_DB,
3636
token: process.env.TERMINUSDB_API_TOKEN,
3737
}
3838
)
@@ -46,7 +46,7 @@ export async function getStaticProps({ params }) {
4646
query: query,
4747
})
4848
const docResult = docs[0]
49-
let html = await renderMarkdown(docResult["body"]["value"])
50-
const entry = { html: html, document: docResult }
49+
let html = await renderMarkdown(docResult?.["body"]?.["value"] ?? "")
50+
const entry = { html: html, document: docResult ?? null }
5151
return { props: { menu: menu, entry: entry } }
5252
}

src/pages/javascript.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ export default function JavaScript(props) {
7272

7373
export async function getStaticProps(context) {
7474
const client = new TerminusClient.WOQLClient(
75-
"https://cloud.terminusdb.com/TerminatorsX",
75+
process.env.TERMINUSDB_API_ENDPOINT,
7676
{
7777
78-
organization: "TerminatorsX",
79-
db: "terminusCMS_docs",
78+
organization: process.env.TERMINUSDB_TEAM,
79+
db: process.env.TERMINUSDB_DB,
8080
token: process.env.TERMINUSDB_API_TOKEN,
8181
}
8282
)
@@ -97,7 +97,7 @@ export async function getStaticProps(context) {
9797
// provide entry slug
9898

9999
const application = await axios.post(
100-
"https://cloud.terminusdb.com/TerminatorsX/api/graphql/TerminatorsX/terminusCMS_docs",
100+
`${process.env.TERMINUSDB_API_ENDPOINT}/api/graphql/${process.env.TERMINUSDB_TEAM}/${process.env.TERMINUSDB_DB}`,
101101
{
102102
query: `query {
103103
Application(filter: {language: {eq: Javascript} } ) {

src/pages/python.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export async function getStaticProps(context) {
103103
}
104104

105105
const application = await axios.post(
106-
"https://cloud.terminusdb.com/TerminatorsX/api/graphql/TerminatorsX/terminusCMS_docs",
106+
`${process.env.TERMINUSDB_API_ENDPOINT}/api/graphql/${process.env.TERMINUSDB_TEAM}/${process.env.TERMINUSDB_DB}`,
107107
{
108108
query: `query {
109109
Application(filter: {language: {eq: Python} } ) {

src/prebuild/menu.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ async function fetchMenu() {
77
headers: { Authorization: `Token ${process.env.TERMINUSDB_API_TOKEN}` },
88
}
99
const req = await axios.post(
10-
"https://cloud.terminusdb.com/TerminatorsX/api/graphql/TerminatorsX/terminusCMS_docs",
10+
`${process.env.TERMINUSDB_API_ENDPOINT}/api/graphql/${process.env.TERMINUSDB_TEAM}/${process.env.TERMINUSDB_DB}`,
1111
{
1212
query: `query {
1313
Menu(orderBy: {Order:ASC}) {

0 commit comments

Comments
 (0)