@@ -33,30 +33,29 @@ XlsxPopulate.fromFileAsync('./SanitizedInformationArchitecture.xlsx')
3333 fileName = fileName + '/index.md'
3434 }
3535
36- const topicsArray = topics . replace ( ', ' , ',' ) . split ( ',' ) || [ ]
36+ const topicsArray = topics . split ( ',' ) . map ( topic => topic . trim ( ) ) || [ ]
3737 updateFrontmatter ( path . join ( process . cwd ( ) , fileName ) , topicsArray )
3838 }
3939 } )
4040
4141function updateFrontmatter ( filePath , newTopics ) {
42- try {
43- const articleContents = fs . readFileSync ( filePath , 'utf8' )
44- const { content, data } = readFrontmatter ( articleContents )
45-
46- let topics = [ ]
47- if ( typeof data . topics === 'string' ) {
48- topics = [ data . topics ]
49- } else if ( Array . isArray ( data . topics ) ) {
50- topics = topics . concat ( data . topics )
51- }
52- newTopics . forEach ( topic => {
53- topics . push ( topic )
54- } )
55- data . topics = topics
56-
57- const newContents = readFrontmatter . stringify ( content , data , { lineWidth : 10000 } )
58- fs . writeFileSync ( filePath , newContents )
59- } catch ( error ) {
60- console . log ( `This file is not valid: ${ filePath } ` )
42+ const articleContents = fs . readFileSync ( filePath , 'utf8' )
43+ const { content, data } = readFrontmatter ( articleContents )
44+
45+ let topics = [ ]
46+ if ( typeof data . topics === 'string' ) {
47+ topics = [ data . topics ]
48+ } else if ( Array . isArray ( data . topics ) ) {
49+ topics = topics . concat ( data . topics )
6150 }
51+ newTopics . forEach ( topic => {
52+ topics . push ( topic )
53+ } )
54+
55+ // remove any duplicates
56+ const uniqueTopics = [ ...new Set ( topics ) ]
57+ data . topics = uniqueTopics
58+
59+ const newContents = readFrontmatter . stringify ( content , data , { lineWidth : 10000 } )
60+ fs . writeFileSync ( filePath , newContents )
6261}
0 commit comments