Skip to content

Commit b79bab1

Browse files
committed
Fix issue in syncing new notes
1 parent 3f3bea6 commit b79bab1

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

github/index.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,28 +166,45 @@ module.exports = class GithubHelper {
166166
remotePath
167167
}];
168168

169+
const metadataToPublish = {
170+
fileName: remotePath,
171+
title
172+
};
173+
169174
const metadata = await this.fetchOrCreateSyncMetadata();
170175
const fileMetaRecord = metadata.notes.find(note => note.fileName === remotePath);
171-
if (!fileMetaRecord) {
176+
const isNewNote = !fileMetaRecord;
177+
const isTitleChanged = !isNewNote && fileMetaRecord.title !== metadataToPublish.title;
178+
179+
if (isNewNote) {
172180
this.logger.debug(`File ${remotePath} is not in metadata. Updating metadata.`);
173181
metadata.lastModified = new Date().toISOString();
174182
metadata.notes = [
175183
...metadata.notes,
176-
{
177-
fileName: remotePath,
178-
title
179-
}
184+
metadataToPublish
180185
];
186+
} else if (isTitleChanged) {
187+
this.logger.debug(`Note details already exist in metadata, but the note title has been changed to ${title}`);
188+
metadata.lastModified = new Date().toISOString();
189+
metadata.notes = metadata.notes.map((curr) => {
190+
if (curr.fileName === remotePath) {
191+
return { ...curr, title };
192+
}
193+
return curr;
194+
});
195+
}
181196

182-
// Add metadata file to be published to github
197+
if (isNewNote || isTitleChanged) {
198+
/* If the note is new or metadata has been changed, metadata should be republished,
199+
* and the table of contents should be updated.
200+
*/
183201
objectsToPublish.push({
184202
content: JSON.stringify(metadata),
185203
remotePath: this.repoConfig.metadataFile
186204
});
187205

188206
this.logger.debug('Re-building table of contents');
189207
const readMeContent = this.generateReadMe(metadata);
190-
// Add readme file to be published to github
191208
objectsToPublish.push({
192209
content: readMeContent,
193210
remotePath: 'README.md'

0 commit comments

Comments
 (0)