Skip to content

Commit

Permalink
feat: support custom formatting on markdown2docx
Browse files Browse the repository at this point in the history
  • Loading branch information
ReinderVosDeWael committed Jul 23, 2024
1 parent 60d79cb commit f500790
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
17 changes: 14 additions & 3 deletions src/routes/api/markdown2docx/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,22 @@ export async function POST({ fetch, request }) {
const headers = new Headers({
"x-functions-key": AZURE_FUNCTION_PYTHON_KEY || ""
})

const formData = await request.formData()
const markdown = formData.get("markdown")
if (!markdown) {
return new Response(null, { status: 400 })
}
const formatting = formData.get("formatting") || null
const body = JSON.stringify({
markdown,
formatting
})

return await fetch(`${AZURE_FUNCTION_PYTHON_URL}/markdown2docx/`, {
method: "POST",
headers: headers,
body: JSON.stringify({
markdown: await request.text()
})
body: body
})
.then(async response => {
if (response.ok && response.body) {
Expand All @@ -21,6 +31,7 @@ export async function POST({ fetch, request }) {
}
})
.catch(error => {
console.log(error)
logger.error("Error converting markdown to docx:", error)
return new Response(null, { status: 500 })
})
Expand Down
14 changes: 9 additions & 5 deletions src/routes/templates/Checkout/checkoutUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ export function getTemplateValues(text: string): TemplateValue[] {
}

export async function submitMarkdownToDocx(markdown: string, rules: string[]) {
const form = new FormData()
form.append("text", markdown)
form.append("rules", rules.join(","))
const languageToolForm = new FormData()
languageToolForm.append("text", markdown)
languageToolForm.append("rules", rules.join(","))

let text: string
if (rules.length === 0) {
text = markdown
} else {
const languageToolResponse = await fetch("/api/language-tool", {
method: "POST",
body: form
body: languageToolForm
})

if (!languageToolResponse.ok) {
Expand All @@ -55,9 +55,13 @@ export async function submitMarkdownToDocx(markdown: string, rules: string[]) {
}

text = giveMarkdownUrlsHyperlinks(text)
const markdown2DocxForm = new FormData()
markdown2DocxForm.append("markdown", text)
markdown2DocxForm.append("formatting", JSON.stringify({ space_before: 0, space_after: 0 }))

const docxResponse = await fetch("/api/markdown2docx", {
method: "POST",
body: text
body: markdown2DocxForm
})
if (!docxResponse.ok) {
throw new Error(await docxResponse.text())
Expand Down
6 changes: 4 additions & 2 deletions src/routes/templates/ExportTemplates.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
return
}
isLoading = true
let markdown = nodesToMarkdown(nodes)
const markdown = nodesToMarkdown(nodes)
const form = new FormData()
form.append("markdown", markdown)
fetch("/api/markdown2docx", {
method: "POST",
body: markdown
body: form
})
.then(async res => {
if (!res.ok) {
Expand Down

0 comments on commit f500790

Please sign in to comment.