Skip to content

Commit 2e3ba81

Browse files
committed
Add discussion logic for snippets
1 parent 0429ada commit 2e3ba81

File tree

5 files changed

+26
-0
lines changed

5 files changed

+26
-0
lines changed

spec/models/snippet.test.js

+13
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ describe('Snippet', () => {
2929
id: 'js/s/my-snippet',
3030
tags: 'array;object',
3131
tokens: 'lorem;ipsum',
32+
discussion: 2020,
3233
});
3334

3435
const nodeJsSnippet = SnippetFactory.create('taggedNodeJs', {
@@ -129,6 +130,18 @@ describe('Snippet', () => {
129130
});
130131
});
131132

133+
describe('discussionUrl', () => {
134+
it('returns discussion URL if discussion exists', () => {
135+
expect(jsSnippet.discussionUrl).toEqual(
136+
`https://github.com/Chalarangelo/30-seconds-of-code/discussions/${jsSnippet.discussion}`
137+
);
138+
});
139+
140+
it('returns null if no discussion', () => {
141+
expect(snippetWithoutLanguage.discussionUrl).toEqual(null);
142+
});
143+
});
144+
132145
describe('isScheduled', () => {
133146
it('returns false if not scheduled', () => {
134147
expect(publishedSnippet.isScheduled).toEqual(false);

src/config/settings.js

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ const settings = {
1616
url: 'https://github.com/Chalarangelo/30-seconds-of-code',
1717
snippetPrefix:
1818
'https://github.com/Chalarangelo/30-seconds-of-code/blob/master/content/snippets',
19+
discussionPrefix:
20+
'https://github.com/Chalarangelo/30-seconds-of-code/discussions/',
1921
},
2022
license: {
2123
url: 'https://creativecommons.org/licenses/by/4.0/',

src/lib/contentUtils/modelWorkers/snippet.js

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export const extractSnippetData = async (snippetGlob, languageData) => {
2323
dateModified,
2424
listed,
2525
tocEnabled = true,
26+
discussion = null,
2627
} = snippet;
2728

2829
const language = languageData.get(languageKey);
@@ -60,6 +61,7 @@ export const extractSnippetData = async (snippetGlob, languageData) => {
6061
languageKey,
6162
tokens: tokens.join(';'),
6263
ranking,
64+
discussion,
6365
};
6466
})
6567
);
@@ -83,6 +85,7 @@ export const exportSnippetData = snippetData => {
8385
languageId: snippet.languageKey,
8486
tokens: snippet.tokens,
8587
ranking: snippet.ranking,
88+
discussion: snippet.discussion,
8689
};
8790
});
8891
/* eslint-enable camelcase */

src/models/snippet.js

+7
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export default class Snippet extends ContentModel {
2929
this.dateModified = new Date(data.dateModified);
3030
this.tableOfContents = data.tableOfContents;
3131
this.languageId = data.languageId;
32+
this.discussion = data.discussion;
3233
}
3334

3435
static byNew(records) {
@@ -117,6 +118,12 @@ export default class Snippet extends ContentModel {
117118
return `${settings.repository.snippetPrefix}${this.slug}.md`;
118119
}
119120

121+
get discussionUrl() {
122+
if (!this.discussion) return null;
123+
124+
return `${settings.repository.discussionPrefix}${this.discussion}`;
125+
}
126+
120127
get isScheduled() {
121128
return this.dateModified > new Date();
122129
}

src/serializers/snippetContextSerializer.js

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export default class SnippetContextSerializer extends Serializer {
1212
['cover', object => object.coverUrlFullSize],
1313
['coverSrcset', object => object.coverSrcsetFullSize],
1414
'githubUrl',
15+
'discussionUrl',
1516
'tableOfContents',
1617
]);
1718
}

0 commit comments

Comments
 (0)