-
-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add script to deploy markdown content
- Loading branch information
Showing
1 changed file
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#!/usr/bin/env node | ||
|
||
const path = require('path') | ||
const fs = require('fs') | ||
|
||
const API_KEY = process.env.SN_API_KEY | ||
|
||
const parseFrontMatter = (content) => { | ||
const lines = content.split('\n') | ||
if (lines[0] !== '---') { | ||
throw new Error('failed to parse front matter: start delimiter not found') | ||
} | ||
|
||
const endIndex = lines.findIndex((line, i) => i > 0 && line === '---') | ||
if (endIndex === -1) { | ||
throw new Error('failed to parse front matter: end delimiter not found') | ||
} | ||
|
||
const meta = {} | ||
for (let i = 1; i < endIndex; i++) { | ||
const line = lines[i] | ||
const [key, ...valueParts] = line.split(':') | ||
if (key && valueParts.length) { | ||
meta[key.trim()] = valueParts.join(':').trim() | ||
} | ||
} | ||
|
||
return meta | ||
} | ||
|
||
const readItem = (name) => { | ||
const content = fs.readFileSync(path.join(__dirname, name), 'utf8') | ||
const lines = content.split('\n') | ||
const startIndex = lines.findIndex((line, i) => i > 0 && line.startsWith('---')) + 1 | ||
return { | ||
...parseFrontMatter(content), | ||
text: lines.slice(startIndex).join('\n') | ||
} | ||
} | ||
|
||
async function upsertDiscussion (variables) { | ||
const response = await fetch('http://localhost:3000/api/graphql', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'X-API-Key': API_KEY | ||
}, | ||
body: JSON.stringify({ | ||
query: ` | ||
mutation upsertDiscussion($id: ID!, $sub: String!; $title: String!, $text: String!) { | ||
upsertDiscussion(id: $id, sub: $sub, title: $title, text: $text) { | ||
result { | ||
id | ||
} | ||
} | ||
} | ||
`, | ||
variables | ||
}) | ||
}) | ||
|
||
const json = await response.json() | ||
if (json.errors) { | ||
throw new Error(json.errors[0].message) | ||
} | ||
|
||
return json.data | ||
} | ||
|
||
const faq = readItem('faq.md') | ||
|
||
upsertDiscussion(faq) |