Skip to content

Commit 8a367dc

Browse files
mportiz08hcz
andauthored
[6.0] Include code and referenced text in metadata description (#862) (#864)
Include code and referenced text in metadata description (#862) Resolves: rdar://129801569, rdar://130035323 Co-authored-by: Sergei Osipov <[email protected]>
1 parent 3e2a65c commit 8a367dc

File tree

4 files changed

+68
-3
lines changed

4 files changed

+68
-3
lines changed

src/components/ContentNode.vue

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,13 +673,21 @@ export default {
673673
// without any inline formatting—other block kinds like asides will be
674674
// ignored in the resulting plaintext representation.
675675
plaintext() {
676+
const { references = {} } = this;
676677
return this.reduce((text, node) => {
677678
if (node.type === BlockType.paragraph) {
678679
return `${text}\n`;
679680
}
681+
if (node.type === InlineType.codeVoice) {
682+
return `${text}${node.code}`;
683+
}
680684
if (node.type === InlineType.text) {
681685
return `${text}${node.text}`;
682686
}
687+
if (node.type === InlineType.reference) {
688+
const title = references[node.identifier]?.title ?? '';
689+
return `${text}${title}`;
690+
}
683691
return text;
684692
}, '').trim();
685693
},

src/mixins/metadata.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ export default {
1818
// Extracts the first paragraph of plaintext from the given content tree,
1919
// which can be used for metadata purposes.
2020
extractFirstParagraphText(content = []) {
21+
const { references = {} } = this;
2122
const plaintext = ContentNode.computed.plaintext.bind({
2223
...ContentNode.methods,
2324
content,
25+
references,
2426
})();
2527
return firstParagraph(plaintext);
2628
},

tests/unit/components/ContentNode.spec.js

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2538,10 +2538,58 @@ describe('ContentNode', () => {
25382538
},
25392539
],
25402540
},
2541+
{
2542+
type: ContentNode.BlockType.paragraph,
2543+
inlineContent: [
2544+
{
2545+
type: ContentNode.InlineType.codeVoice,
2546+
code: 'C',
2547+
},
2548+
],
2549+
},
2550+
],
2551+
},
2552+
});
2553+
expect(wrapper.vm.plaintext).toBe('A\nB\nC');
2554+
});
2555+
2556+
it('includes text from references', () => {
2557+
const wrapper = shallowMount(ContentNode, {
2558+
propsData: {
2559+
content: [
2560+
{
2561+
type: ContentNode.BlockType.paragraph,
2562+
inlineContent: [
2563+
{
2564+
type: ContentNode.InlineType.text,
2565+
text: 'A',
2566+
},
2567+
{
2568+
type: ContentNode.InlineType.text,
2569+
text: ' ',
2570+
},
2571+
{
2572+
type: ContentNode.InlineType.reference,
2573+
identifier: 'b',
2574+
},
2575+
],
2576+
},
25412577
],
25422578
},
2579+
provide: {
2580+
store: {
2581+
state: {
2582+
references: {
2583+
b: {
2584+
title: 'B',
2585+
url: '/b',
2586+
},
2587+
},
2588+
},
2589+
},
2590+
},
25432591
});
2544-
expect(wrapper.vm.plaintext).toBe('A\nB');
2592+
expect(wrapper.vm.plaintext).toBe('A B');
25452593
});
25462594
});
25472595
});

tests/unit/mixins/metadata.spec.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ const createWrapper = ({
4545
disableMetadata: () => disableMetadata,
4646
pageTitle: () => title,
4747
pageDescription: () => description,
48+
references: () => ({
49+
'ref-d': { title: 'd' },
50+
}),
4851
},
4952
}, {
5053
mocks: {
@@ -99,7 +102,11 @@ describe('metadata', () => {
99102
},
100103
{
101104
type: 'text',
102-
text: ' c',
105+
text: ' c ',
106+
},
107+
{
108+
type: 'reference',
109+
identifier: 'ref-d',
103110
},
104111
],
105112
},
@@ -114,7 +121,7 @@ describe('metadata', () => {
114121
},
115122
];
116123
const wrapper = createWrapper(pageData);
117-
expect(wrapper.vm.extractFirstParagraphText(content)).toBe('a b c');
124+
expect(wrapper.vm.extractFirstParagraphText(content)).toBe('a b c d');
118125
expect(wrapper.vm.extractFirstParagraphText([])).toBe('');
119126
});
120127
});

0 commit comments

Comments
 (0)