• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

evolvedbinary / prosemirror-lwdita / ca0795a3-62a9-47ce-a64f-eee52c7a31d6

19 Sep 2024 12:07PM UTC coverage: 43.394% (-14.9%) from 58.299%
ca0795a3-62a9-47ce-a64f-eee52c7a31d6

push

circleci

marmoure
[feature] fetch the document from github and transform to prosemirror json then render it

169 of 357 branches covered (47.34%)

Branch coverage included in aggregate %.

4 of 7 new or added lines in 1 file covered. (57.14%)

281 of 680 relevant lines covered (41.32%)

29.26 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

63.16
/packages/prosemirror-lwdita/src/github-integration/github.plugin.ts
1
/*!
2
Copyright (C) 2020 Evolved Binary
3

4
This program is free software: you can redistribute it and/or modify
5
it under the terms of the GNU Affero General Public License as
6
published by the Free Software Foundation, either version 3 of the
7
License, or (at your option) any later version.
8

9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
GNU Affero General Public License for more details.
13

14
You should have received a copy of the GNU Affero General Public License
15
along with this program.  If not, see <https://www.gnu.org/licenses/>.
16
*/
17

18
import { xditaToJdita } from "@evolvedbinary/lwdita-xdita";
1✔
19
import { document as jditaToProsemirrorJson } from "../document";
1✔
20

21
/**
22
 * Fetches the raw content of a document from a GitHub repository.
23
 *
24
 * @param ghrepo - The GitHub repository in the format "owner/repo".
25
 * @param source - The path to the file within the repository.
26
 * @returns A promise that resolves to the raw content of the document as a string.
27
 *
28
 * @remarks
29
 * This function currently fetches the document from the 'main' branch of the repository.
30
 * should use the GitHub API to dynamically determine the default branch of the repository.
31
 */
32
export const fetchRawDocumentFromGitHub = async (ghrepo: string, source: string): Promise<string> => {
2✔
33
  //TODO(YB): the branch should be passed as a parameter
34
  const url = `https://raw.githubusercontent.com/${ghrepo}/main/${source}`;
2✔
35
  const response = await fetch(url);
2✔
36

37
  //TODO: Handle errors
38
  return response.text();
2✔
39
};
40

41
/**
42
 * Transforms a raw GitHub document into a ProseMirror state save.
43
 *
44
 * @param rawDocument - The raw xdita document as a string.
45
 * @returns A promise that resolves to a record containing the ProseMirror state save.
46
 */
47
// eslint-disable-next-line @typescript-eslint/no-explicit-any
48
export const transformGitHubDocumentToProsemirrorJson = async (rawDocument: string): Promise<Record<string, any>> => {
1✔
49
    // convert the raw xdita document to jdita
50
    const jdita = await xditaToJdita(rawDocument);
1✔
51
    
52
    // convert the jdita document to prosemirror state save
53
    const prosemirrorJson = await jditaToProsemirrorJson(jdita);
1✔
54
    
55
    return prosemirrorJson;
1✔
56
};
57

58
/**
59
 * Fetches a raw document from a GitHub repository and transforms it into a ProseMirror JSON document.
60
 *
61
 * @param ghrepo - The GitHub repository from which to fetch the document.
62
 * @param source - The source path of the document within the repository.
63
 * @returns A promise that resolves to the transformed ProseMirror JSON document.
64
 */
65
export const fetchAndTransform = async (ghrepo: string, source: string) => {
1✔
NEW
66
  const rawDoc = await fetchRawDocumentFromGitHub(ghrepo, source);
×
NEW
67
  const jsonDoc = await transformGitHubDocumentToProsemirrorJson(rawDoc);
×
NEW
68
  return jsonDoc;
×
69
};
70

71
/**
72
 * Exchanges an OAuth code for an access token.
73
 *
74
 * @param code - The OAuth code to exchange for an access token.
75
 * @returns A promise that resolves to the access token as a string.
76
 * @throws Will throw an error if the fetch request fails or if the response is not in the expected format.
77
 */
78
export const exchangeOAuthCodeForAccessToken = async (code: string): Promise<string> => {
1✔
79
  // build the URL to exchange the code for an access token
80
  const url = `http://localhost:3000/api/github/token?code=${code}`;
×
81
  // fetch the access token
82
  const response = await fetch(url);
×
83
  const json = await response.json();
×
84
  //TODO: Handle errors
85
  return json;
×
86
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc