on master help sign in
Merge pull request #278 from ckipp01/feature/merge-same-definitions
116557cЛu Лinveгa
scripts/wiki.js+21−14
@@ -9,27 +9,30 @@ const Wiki = sites => {
9 const authors = new Set()
9 const authors = new Set()
10 const terms = new Set()
10 const terms = new Set()
11
11
12− const storeEntry = (author, url, parent, cat, entry) => {
12+ const storeEntry = (author, url, category, term, entry) => {
13− if (!(parent in entries)) {
13+ if (!(category in entries)) {
14− entries[parent] = {}
14+ entries[category] = {}
15− entries[parent][cat] = [{ entry, author, url }]
15+ entries[category][term] = [{ entry, authors: [{ author, url }] }]
16− } else if (!(cat in entries[parent])) {
16+ } else if (!(term in entries[category])) {
17− entries[parent][cat] = [{ entry, author, url }]
17+ entries[category][term] = [{ entry, authors: [{ author, url }] }]
18 } else {
18 } else {
19+ const possibleIndex = entries[category][term].findIndex(existing => existing.entry === entry)
20+ possibleIndex > -1
21+ ? entries[category][term][possibleIndex].authors.push({ author, url })
19− entries[parent][cat].push({ entry, author, url })
22+ : entries[category][term].push({ entry, authors: [{ author, url }] })
20 }
23 }
21− terms.add(cat)
24+ terms.add(term)
22 authors.add(author)
25 authors.add(author)
23 }
26 }
24
27
25− const storeEntries = (author, url, parent, entries) => {
28+ const storeEntries = (author, url, category, entries) => {
26 Array.isArray(entries)
29 Array.isArray(entries)
27− ? storeEntry(author, url, parent, 'list-definitions', entries)
30+ ? storeEntry(author, url, category, 'list-definitions', entries)
28− : Object.keys(entries).forEach(entry => storeEntry(author, url, parent, entry, entries[entry]))
31+ : Object.keys(entries).forEach(entry => storeEntry(author, url, category, entry, entries[entry]))
29 }
32 }
30
33
31 const transform = (author, url, ndtl) => {
34 const transform = (author, url, ndtl) => {
32− Object.keys(ndtl).forEach(cat => storeEntries(author, url, cat, ndtl[cat]))
35+ Object.keys(ndtl).forEach(category => storeEntries(author, url, category, ndtl[category]))
33 }
36 }
34
37
35 const parse = (site, content) => {
38 const parse = (site, content) => {
@@ -60,15 +63,19 @@ const Wiki = sites => {
60 '<a href="$1" target="_blank" rel="noopener noreferrer">$1</a>'
63 '<a href="$1" target="_blank" rel="noopener noreferrer">$1</a>'
61 )
64 )
62
65
66+ const formatAuthor = (authors, author) => {
67+ return `${authors}<a class='author' target='_blank' href='${author.url}'> @${author.author}</a>`
68+ }
69+
63 const formatEntry = (definitions, definition) => {
70 const formatEntry = (definitions, definition) => {
64 const formatEntryList = entryList => entryList.reduce((items, item) => `${items}<li>${formatLinks(item)}</li>`, '')
71 const formatEntryList = entryList => entryList.reduce((items, item) => `${items}<li>${formatLinks(item)}</li>`, '')
65
72
66 const newHtml = typeof definition.entry === 'string'
73 const newHtml = typeof definition.entry === 'string'
67 ? `<li>${formatLinks(definition.entry)}
74 ? `<li>${formatLinks(definition.entry)}
68− - <a class='author' target='_blank' href='${definition.url}'> @${definition.author}</a>
75+ - ${definition.authors.reduce(formatAuthor, '')}
69 </li>`
76 </li>`
70 : `<li><ul>${formatEntryList(definition.entry)}
77 : `<li><ul>${formatEntryList(definition.entry)}
71− <a class='author' target='_blank' href='${definition.url}'> — @${definition.author}</a>
78+ - ${definition.authors.reduce(formatAuthor, '')}
72 </ul></li>`
79 </ul></li>`
73
80
74 return `${definitions}${newHtml}`
81 return `${definitions}${newHtml}`