-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4352 from serlo/staging
Deployment
- Loading branch information
Showing
86 changed files
with
1,669 additions
and
194 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+4.83 KB
.yarn/cache/html-encoding-sniffer-npm-4.0.0-5f6627070d-3339b71dab.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+4.01 KB
.yarn/cache/isomorphic-dompurify-npm-2.19.0-4f6eb49402-42fb8a4d5e.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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
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
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
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
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
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
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
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
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,101 @@ | ||
import { parseDocumentString } from '@editor/static-renderer/helper/parse-document-string' | ||
import type { | ||
EditorArticleDocument, | ||
EditorExerciseDocument, | ||
EditorExerciseGroupDocument, | ||
} from '@editor/types/editor-plugins' | ||
import { gql } from 'graphql-request' | ||
import type { NextApiRequest, NextApiResponse } from 'next' | ||
|
||
import { endpoint } from '@/api/endpoint' | ||
import { ShareEditorContentQuery } from '@/fetcher/graphql-types/operations' | ||
import { isProduction } from '@/helper/is-production' | ||
|
||
/** | ||
* Allows frontend to copy Serlo content to the clipboard. | ||
* The content is unpacked for consistent pasting in the Editor. | ||
*/ | ||
export default async function handler( | ||
req: NextApiRequest, | ||
res: NextApiResponse | ||
) { | ||
const href = decodeURIComponent(String(req.query.href)) | ||
|
||
const [base] = href.split('#') | ||
const path = base.startsWith('/') ? base : `/${base}` | ||
|
||
if (!path) { | ||
return res.status(401).json('no path provided') | ||
} | ||
|
||
try { | ||
void fetch(endpoint, { | ||
headers: { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json', | ||
}, | ||
method: 'POST', | ||
body: JSON.stringify({ query, variables: { path } }), | ||
}) | ||
.then((res) => res.json()) | ||
.then((data: { data: ShareEditorContentQuery }) => { | ||
if (!data.data?.uuid) { | ||
return res.status(404).json('not found') | ||
} | ||
|
||
const uuid = data.data.uuid | ||
|
||
if (!Object.hasOwn(uuid, 'currentRevision') || !uuid.currentRevision) { | ||
return res.status(404).json('no current revision') | ||
} | ||
|
||
if (uuid.__typename === 'Article') { | ||
const articleDocument = parseDocumentString( | ||
uuid.currentRevision.content | ||
) as EditorArticleDocument | ||
const articleContent = articleDocument.state.content | ||
respondWithContent(articleContent) | ||
return | ||
} | ||
|
||
if ( | ||
uuid.__typename === 'Exercise' || | ||
uuid.__typename === 'ExerciseGroup' | ||
) { | ||
const exercise = parseDocumentString(uuid.currentRevision.content) as | ||
| EditorExerciseDocument | ||
| EditorExerciseGroupDocument | ||
respondWithContent({ plugin: 'rows', state: [exercise] }) | ||
return | ||
} | ||
|
||
return res.status(422).json('unsupported entity type') | ||
}) | ||
.catch((e) => { | ||
return res.status(500).json(`${String(e)} at ${path}`) | ||
}) | ||
} catch (e) { | ||
return res.status(500).json(`${String(e)} at ${path}`) | ||
} | ||
|
||
function respondWithContent(content: any) { | ||
const twoDaysInSeconds = 172800 | ||
res.setHeader('Cache-Control', `maxage=${twoDaysInSeconds}`) | ||
if (!isProduction) res.setHeader('Access-Control-Allow-Origin', '*') | ||
res.status(200).json(content) | ||
} | ||
} | ||
|
||
const query = gql` | ||
query shareEditorContent($path: String!) { | ||
uuid(alias: { path: $path, instance: de }) { | ||
__typename | ||
... on AbstractEntity { | ||
currentRevision { | ||
content | ||
} | ||
} | ||
} | ||
} | ||
` |
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
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
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
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
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
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
Oops, something went wrong.