@@ -49,7 +49,8 @@ async function commit(params) {
4949 treeSHA,
5050 latestCommitSHA,
5151 submission,
52- destinationFolder
52+ destinationFolder,
53+ question_data
5354 } = params ;
5455
5556 const name = normalizeName ( submission . title ) ;
@@ -60,14 +61,21 @@ async function commit(params) {
6061 }
6162
6263 const prefix = ! ! destinationFolder ? `${ destinationFolder } /` : '' ;
63- const path = `${ prefix } problems/${ name } /solution.${ LANG_TO_EXTENSION [ submission . lang ] } `
64+ const questionPath = `${ prefix } problems/${ name } /question.md` ; // Markdown file for the problem with question data
65+ const solutionPath = `${ prefix } problems/${ name } /solution.${ LANG_TO_EXTENSION [ submission . lang ] } ` ; // Separate file for the solution
66+ 6467
6568 const treeData = [
6669 {
67- path,
70+ path : questionPath ,
71+ mode : '100644' ,
72+ content : question_data ,
73+ } ,
74+ {
75+ path : solutionPath ,
6876 mode : '100644' ,
6977 content : submission . code ,
70- }
78+ } ,
7179 ] ;
7280
7381 const treeResponse = await octokit . git . createTree ( {
@@ -109,6 +117,33 @@ async function commit(params) {
109117 return [ treeResponse . data . sha , commitResponse . data . sha ] ;
110118}
111119
120+ async function getQuestionData ( titleSlug , leetcodeSession ) {
121+ log ( `Getting question data for ${ titleSlug } ...` ) ;
122+ 123+ const headers = {
124+ "Content-Type" : "application/json" ,
125+ "Cookie" : `LEETCODE_SESSION=${ leetcodeSession } ;`
126+ }
127+ 128+ const graphql = JSON . stringify ( {
129+ query : `query getQuestionDetail($titleSlug: String!) {
130+ question(titleSlug: $titleSlug) {
131+ content
132+ }
133+ }` ,
134+ variables : { "titleSlug" : titleSlug } ,
135+ } )
136+ 137+ 138+ try {
139+ const response = await axios . post ( "https://leetcode.com/graphql/" , graphql , { headers} ) ;
140+ const result = await response . data ;
141+ return result . data . question . content ;
142+ } catch ( error ) {
143+ console . log ( 'error' , error ) ;
144+ }
145+ }
146+ 112147// Returns false if no more submissions should be added.
113148function addToSubmissions ( params ) {
114149 const {
@@ -242,7 +277,10 @@ async function sync(inputs) {
242277 let treeSHA = commits . data [ 0 ] . commit . tree . sha ;
243278 for ( i = submissions . length - 1 ; i >= 0 ; i -- ) {
244279 submission = submissions [ i ] ;
245- [ treeSHA , latestCommitSHA ] = await commit ( { octokit, owner, repo, defaultBranch, commitInfo, treeSHA, latestCommitSHA, submission, destinationFolder } ) ;
280+ 281+ // Get the question data for the submission.
282+ const question_data = await getQuestionData ( submission . title_slug , leetcodeSession ) ;
283+ [ treeSHA , latestCommitSHA ] = await commit ( { octokit, owner, repo, defaultBranch, commitInfo, treeSHA, latestCommitSHA, submission, destinationFolder, question_data } ) ;
246284 }
247285 log ( 'Done syncing all submissions.' ) ;
248286}
0 commit comments