Skip to content

[6.0] Include code and referenced text in metadata description (#862) #864

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/components/ContentNode.vue
Original file line number Diff line number Diff line change
Expand Up @@ -673,13 +673,21 @@ export default {
// without any inline formatting—other block kinds like asides will be
// ignored in the resulting plaintext representation.
plaintext() {
const { references = {} } = this;
return this.reduce((text, node) => {
if (node.type === BlockType.paragraph) {
return `${text}\n`;
}
if (node.type === InlineType.codeVoice) {
return `${text}${node.code}`;
}
if (node.type === InlineType.text) {
return `${text}${node.text}`;
}
if (node.type === InlineType.reference) {
const title = references[node.identifier]?.title ?? '';
return `${text}${title}`;
}
return text;
}, '').trim();
},
Expand Down
2 changes: 2 additions & 0 deletions src/mixins/metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ export default {
// Extracts the first paragraph of plaintext from the given content tree,
// which can be used for metadata purposes.
extractFirstParagraphText(content = []) {
const { references = {} } = this;
const plaintext = ContentNode.computed.plaintext.bind({
...ContentNode.methods,
content,
references,
})();
return firstParagraph(plaintext);
},
Expand Down
50 changes: 49 additions & 1 deletion tests/unit/components/ContentNode.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2538,10 +2538,58 @@ describe('ContentNode', () => {
},
],
},
{
type: ContentNode.BlockType.paragraph,
inlineContent: [
{
type: ContentNode.InlineType.codeVoice,
code: 'C',
},
],
},
],
},
});
expect(wrapper.vm.plaintext).toBe('A\nB\nC');
});

it('includes text from references', () => {
const wrapper = shallowMount(ContentNode, {
propsData: {
content: [
{
type: ContentNode.BlockType.paragraph,
inlineContent: [
{
type: ContentNode.InlineType.text,
text: 'A',
},
{
type: ContentNode.InlineType.text,
text: ' ',
},
{
type: ContentNode.InlineType.reference,
identifier: 'b',
},
],
},
],
},
provide: {
store: {
state: {
references: {
b: {
title: 'B',
url: '/b',
},
},
},
},
},
});
expect(wrapper.vm.plaintext).toBe('A\nB');
expect(wrapper.vm.plaintext).toBe('A B');
});
});
});
11 changes: 9 additions & 2 deletions tests/unit/mixins/metadata.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ const createWrapper = ({
disableMetadata: () => disableMetadata,
pageTitle: () => title,
pageDescription: () => description,
references: () => ({
'ref-d': { title: 'd' },
}),
},
}, {
mocks: {
Expand Down Expand Up @@ -99,7 +102,11 @@ describe('metadata', () => {
},
{
type: 'text',
text: ' c',
text: ' c ',
},
{
type: 'reference',
identifier: 'ref-d',
},
],
},
Expand All @@ -114,7 +121,7 @@ describe('metadata', () => {
},
];
const wrapper = createWrapper(pageData);
expect(wrapper.vm.extractFirstParagraphText(content)).toBe('a b c');
expect(wrapper.vm.extractFirstParagraphText(content)).toBe('a b c d');
expect(wrapper.vm.extractFirstParagraphText([])).toBe('');
});
});
Expand Down