11const axios = require ( 'axios' ) ;
22const { Octokit } = require ( '@octokit/rest' ) ;
3+ const fetch = require ( "node-fetch" ) ;
34
45const COMMIT_MESSAGE = 'Sync LeetCode submission' ;
56const LANG_TO_EXTENSION = {
@@ -45,7 +46,8 @@ async function commit(params) {
4546 treeSHA,
4647 latestCommitSHA,
4748 submission,
48- destinationFolder
49+ destinationFolder,
50+ question_data
4951 } = params ;
5052
5153 const name = normalizeName ( submission . title ) ;
@@ -56,13 +58,13 @@ async function commit(params) {
5658 }
5759
5860 const prefix = ! ! destinationFolder ? `${ destinationFolder } /` : '' ;
59- const path = `${ prefix } problems/${ name } /solution.${ LANG_TO_EXTENSION [ submission . lang ] } `
61+ const path = `${ prefix } problems/${ name } /solution.md` // Markdown file for the problem with the solution at the end
6062
6163 const treeData = [
6264 {
6365 path,
6466 mode : '100644' ,
65- content : submission . code ,
67+ content : question_data + '\n' + '# Solution' + '\n' + '```' + submission . lang + ' \n' + submission . code + '\n' + '```' ,
6668 }
6769 ] ;
6870
@@ -105,6 +107,40 @@ async function commit(params) {
105107 return [ treeResponse . data . sha , commitResponse . data . sha ] ;
106108}
107109
110+ async function getQuestionData ( titleSlug , leetcodeSession ) {
111+ log ( `Getting question data for ${ titleSlug } ...` ) ;
112+
113+ const headers = {
114+ "Content-Type" : "application/json" ,
115+ "Cookie" : `LEETCODE_SESSION=${ leetcodeSession } ;`
116+ }
117+
118+ const graphql = JSON . stringify ( {
119+ query : `query getQuestionDetail($titleSlug: String!) {
120+ question(titleSlug: $titleSlug) {
121+ content
122+ }
123+ }` ,
124+ variables : { "titleSlug" : titleSlug } ,
125+ } )
126+
127+ const requestOptions = {
128+ method : 'POST' ,
129+ headers,
130+ body : graphql ,
131+ redirect : 'follow'
132+ } ;
133+
134+ try {
135+ const response = await fetch ( "https://leetcode.com/graphql/" , requestOptions ) ;
136+ const result = await response . text ( ) ;
137+ const parsedResult = JSON . parse ( result ) ;
138+ return parsedResult . data . question . content ;
139+ } catch ( error ) {
140+ console . log ( 'error' , error ) ;
141+ }
142+ }
143+
108144// Returns false if no more submissions should be added.
109145function addToSubmissions ( params ) {
110146 const {
@@ -238,7 +274,10 @@ async function sync(inputs) {
238274 let treeSHA = commits . data [ 0 ] . commit . tree . sha ;
239275 for ( i = submissions . length - 1 ; i >= 0 ; i -- ) {
240276 submission = submissions [ i ] ;
241- [ treeSHA , latestCommitSHA ] = await commit ( { octokit, owner, repo, defaultBranch, commitInfo, treeSHA, latestCommitSHA, submission, destinationFolder } ) ;
277+
278+ // Get the question data for the submission.
279+ const question_data = await getQuestionData ( submission . title_slug , leetcodeSession ) ;
280+ [ treeSHA , latestCommitSHA ] = await commit ( { octokit, owner, repo, defaultBranch, commitInfo, treeSHA, latestCommitSHA, submission, destinationFolder, question_data } ) ;
242281 }
243282 log ( 'Done syncing all submissions.' ) ;
244283}
0 commit comments