Skip to content

Commit

Permalink
Add script to deploy markdown content
Browse files Browse the repository at this point in the history
  • Loading branch information
ekzyis committed Jan 5, 2025
1 parent 98e669a commit 053493a
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions markdown/deploy.js
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)

0 comments on commit 053493a

Please sign in to comment.