@@ -47,7 +47,8 @@ async function commit(params) {
4747 treeSHA,
4848 latestCommitSHA,
4949 submission,
50- destinationFolder
50+ destinationFolder,
51+ question_data
5152 } = params ;
5253
5354 const name = normalizeName ( submission . title ) ;
@@ -58,14 +59,21 @@ async function commit(params) {
5859 }
5960
6061 const prefix = ! ! destinationFolder ? `${ destinationFolder } /` : '' ;
61- const path = `${ prefix } problems/${ name } /solution.${ LANG_TO_EXTENSION [ submission . lang ] } `
62+ const questionPath = `${ prefix } problems/${ name } /question.md` ; // Markdown file for the problem with question data
63+ const solutionPath = `${ prefix } problems/${ name } /solution.${ LANG_TO_EXTENSION [ submission . lang ] } ` ; // Separate file for the solution
64+ 6265
6366 const treeData = [
6467 {
65- path,
68+ path : questionPath ,
69+ mode : '100644' ,
70+ content : question_data ,
71+ } ,
72+ {
73+ path : solutionPath ,
6674 mode : '100644' ,
6775 content : submission . code ,
68- }
76+ } ,
6977 ] ;
7078
7179 const treeResponse = await octokit . git . createTree ( {
@@ -107,6 +115,33 @@ async function commit(params) {
107115 return [ treeResponse . data . sha , commitResponse . data . sha ] ;
108116}
109117
118+ async function getQuestionData ( titleSlug , leetcodeSession ) {
119+ log ( `Getting question data for ${ titleSlug } ...` ) ;
120+ 121+ const headers = {
122+ "Content-Type" : "application/json" ,
123+ "Cookie" : `LEETCODE_SESSION=${ leetcodeSession } ;`
124+ }
125+ 126+ const graphql = JSON . stringify ( {
127+ query : `query getQuestionDetail($titleSlug: String!) {
128+ question(titleSlug: $titleSlug) {
129+ content
130+ }
131+ }` ,
132+ variables : { "titleSlug" : titleSlug } ,
133+ } )
134+ 135+ 136+ try {
137+ const response = await axios . post ( "https://leetcode.com/graphql/" , graphql , { headers} ) ;
138+ const result = await response . data ;
139+ return result . data . question . content ;
140+ } catch ( error ) {
141+ console . log ( 'error' , error ) ;
142+ }
143+ }
144+ 110145// Returns false if no more submissions should be added.
111146function addToSubmissions ( params ) {
112147 const {
@@ -240,7 +275,10 @@ async function sync(inputs) {
240275 let treeSHA = commits . data [ 0 ] . commit . tree . sha ;
241276 for ( i = submissions . length - 1 ; i >= 0 ; i -- ) {
242277 submission = submissions [ i ] ;
243- [ treeSHA , latestCommitSHA ] = await commit ( { octokit, owner, repo, defaultBranch, commitInfo, treeSHA, latestCommitSHA, submission, destinationFolder } ) ;
278+ 279+ // Get the question data for the submission.
280+ const question_data = await getQuestionData ( submission . title_slug , leetcodeSession ) ;
281+ [ treeSHA , latestCommitSHA ] = await commit ( { octokit, owner, repo, defaultBranch, commitInfo, treeSHA, latestCommitSHA, submission, destinationFolder, question_data } ) ;
244282 }
245283 log ( 'Done syncing all submissions.' ) ;
246284}
0 commit comments