diff --git a/package-lock.json b/package-lock.json index 22a818ba..884b2d72 100644 --- a/package-lock.json +++ b/package-lock.json @@ -43,7 +43,9 @@ "remark-mdx-frontmatter": "^3.0.0", "remark-unwrap-images": "^3.0.1", "sharp": "^0.31.3", - "tls": "^0.0.1" + "tls": "^0.0.1", + "vfile": "^5.3.7", + "vfile-message": "^3.1.4" }, "devDependencies": { "@codedependant/semantic-release-docker": "^4.3.0", diff --git a/package.json b/package.json index 7e944ad9..54c5712b 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,9 @@ "remark-mdx-frontmatter": "^3.0.0", "remark-unwrap-images": "^3.0.1", "sharp": "^0.31.3", - "tls": "^0.0.1" + "tls": "^0.0.1", + "vfile": "^5.3.7", + "vfile-message": "^3.1.4" }, "devDependencies": { "@codedependant/semantic-release-docker": "^4.3.0", diff --git a/pages/api/etherpad/pad.js b/pages/api/etherpad/pad.js new file mode 100644 index 00000000..5abf76c6 --- /dev/null +++ b/pages/api/etherpad/pad.js @@ -0,0 +1,24 @@ + +export default async function handler(req, res) { + const axios = require('axios'); + const client = axios.create({ + baseURL: process.env.ETHERPAD_BASE_URL, + timeout: 1000, + params: { 'apikey': process.env.ETHERPAD_API_KEY }, + }); + let pad = null; + try { + let resp = (await client.get('getText', { + params: { + padID: req.query.pad, + rev: req.query.rev, + } + })) + pad = resp.data.data?.text.text + } catch (error) { + console.log(error) + res.status(500).json({error: 'error fetching pad: ' + error}) + } + + res.status(200).json({ content: pad }) +} \ No newline at end of file diff --git a/pages/api/files/file.js b/pages/api/files/file.js index d46d0af6..7d690484 100644 --- a/pages/api/files/file.js +++ b/pages/api/files/file.js @@ -22,7 +22,7 @@ export default async function handler(req, res) { console.log('api:file Error: ', error) // Return an error response if the file couldn't be read - res.status(500).json({ error: 'Failed to read the file.' }); + res.status(500).json({ error: 'Failed to read the file. :' + error }); } diff --git a/pages/index.js b/pages/index.js index ec11a0c2..27a6f032 100644 --- a/pages/index.js +++ b/pages/index.js @@ -59,8 +59,10 @@ return ( - - + {/* */} + {/* */} + + @@ -138,7 +140,8 @@ export default function Home() { justifyContent="center" > - + + diff --git a/pages/output/[...parms].jsx b/pages/output/[...parms].jsx index 9f887bf9..152f2665 100644 --- a/pages/output/[...parms].jsx +++ b/pages/output/[...parms].jsx @@ -1,4 +1,4 @@ -import React, { useState, useEffect, useLayoutEffect, useCallback, useRef } from 'react' +import React, { useState, useEffect, useCallback, useRef } from 'react' import { VFile } from 'vfile' import { VFileMessage } from 'vfile-message' import * as provider from '@mdx-js/react' @@ -30,7 +30,7 @@ function removeSection(pad, tagName) { function useMdx(defaults) { const [state, setState] = useState({ ...defaults, file: null }) - + const { run: setConfig } = useDebounceFn( async (config) => { const file = new VFile({ basename: 'example.mdx', value: config.value }) @@ -58,7 +58,7 @@ function useMdx(defaults) { remarkPlugins, // rehypePlugins: [capture('hast')], // recmaPlugins: [capture('esast')], - + }) ).default } catch (error) { @@ -111,6 +111,9 @@ export default dynamic(() => Promise.resolve(Page), { function Page() { const router = useRouter(); + const [refreshToken, setRefreshToken] = useState(Math.random()); + const [rev, setRev] = useState(0); + let format = 'default'; if (router.query.format) { @@ -128,7 +131,7 @@ function Page() { # No Content Loaded `; - const [state, setConfig] = useMdx({ + const [state, setConfig] = useMdx({ gfm: true, frontmatter: true, math: false, @@ -138,14 +141,14 @@ function Page() { const mdxContent = (format, mdx, pageParms) => { console.log('pageParms: ', pageParms) - if (pageParms && pageParms.parms) { delete pageParms.parms}; - const {content, data} = matter(mdx); - let frontmatter = {...data, ...pageParms}; + if (pageParms && pageParms.parms) { delete pageParms.parms }; + const { content, data } = matter(mdx); + let frontmatter = { ...data, ...pageParms }; if (format === 'ppt') { mdx = '\n' + content + '\n' } else if (format === 'pdf') { mdx = '
\n' + content.replace(/---/g, '') + '\n
' - mdx = matter.stringify(mdx, {...frontmatter}); + mdx = matter.stringify(mdx, { ...frontmatter }); } else if (format === 'print') { mdx = '\n' + content + '\n' } else { @@ -154,6 +157,7 @@ function Page() { } return mdx } + // const stats = state.file ? statistics(state.file) : {} useEffect(() => { @@ -177,15 +181,49 @@ function Page() { .catch(error => { console.log(error) return { fileData: null, error: error } + }) + .finally(() => { + setTimeout(() => setRefreshToken(Math.random()), 0); }); } else { console.log('output:error: no source defined') } } - fetchFileContent() - }, [source] + const fetchPadContent = async () => { + fetch(`/api/etherpad/pad-revs?pad=${location}`) + .then((res) => res.json()) + .then(data => { + // console.log('data.rev : ', data.rev , 'rev : ', rev) + if (data.rev && data.rev > rev) { + console.log('new revision :', data.rev) + const newrev = data.rev + fetch(`/api/etherpad/pad?pad=${location}&format=${format}&rev=${newrev}`) + .then((res) => res.json()) + .then(data => { + if (data.content) { + setConfig({ ...state, value: String(mdxContent(format, data.content, router.query)) }); + setRev(newrev); // update the revision after successful fetch + } + }) + .catch(error => { + console.log(error) + }) + } + + }) + .catch(error => { + console.log(error) + }) + .finally(() => { + setTimeout(() => setRefreshToken(Math.random()), 5000); + }); + } + + if (source === 'file') {fetchFileContent()} else if (source === 'pad') {fetchPadContent()} + + }, [refreshToken, source] ); @@ -203,9 +241,9 @@ function Page() { if (format === 'pdf') { if (state.file && state.file.result) { console.log('/output:PrintView:file: ', state.file.result) } return ( - - {state.file && state.file.result ? () : null} - + + {state.file && state.file.result ? () : null} + ) } else { @@ -256,7 +294,7 @@ function PrintView({ children }) { - {children && children} + {children && children}
@@ -272,7 +310,7 @@ function DefaultView({ children }) { return ( - {children && children} + {children && children} ) };