11import { describe , expect , test } from 'vitest' ;
22
33import { Citation } from '@/cohere-client' ;
4- import { fixCitationsLeadingMarkdown , replaceTextWithCitations } from '@/utils' ;
4+ import {
5+ fixInlineCitationsForMarkdown ,
6+ isReferenceBetweenSpecialTags ,
7+ replaceTextWithCitations ,
8+ } from '@/utils' ;
59
610describe ( 'replaceTextWithCitations' , ( ) => {
711 test ( 'should replace text with citations' , ( ) => {
@@ -44,46 +48,6 @@ describe('replaceTextWithCitations', () => {
4448 ) ;
4549 } ) ;
4650
47- test ( 'should avoid to break markdown images' , ( ) => {
48- const citations : Citation [ ] = [
49- {
50- start : 0 ,
51- end : 26 ,
52- text : '! [test](https://test.com)' ,
53- document_ids : [ '12345' ] ,
54- } ,
55- ] ;
56- const text = '' ;
57- const generationId = '12345' ;
58- const result = replaceTextWithCitations ( text , citations , generationId ) ;
59- expect ( result ) . toBe (
60- ':cite[]{generationId="12345" start="0" end="26"}'
61- ) ;
62- } ) ;
63-
64- test ( 'should handle extra space when fixing markdown images' , ( ) => {
65- const citations : Citation [ ] = [
66- {
67- start : 5 ,
68- end : 31 ,
69- text : '! [test](https://test.com)' ,
70- document_ids : [ '12345' ] ,
71- } ,
72- {
73- start : 32 ,
74- end : 39 ,
75- text : 'Kadabra' ,
76- document_ids : [ '44444' ] ,
77- } ,
78- ] ;
79- const text = 'Abra  Kadabra' ;
80- const generationId = '12345' ;
81- const result = replaceTextWithCitations ( text , citations , generationId ) ;
82- expect ( result ) . toBe (
83- 'Abra :cite[]{generationId="12345" start="5" end="31"} :cite[Kadabra]{generationId="12345" start="32" end="39"}'
84- ) ;
85- } ) ;
86-
8751 test ( 'should allow citations as markdown elements' , ( ) => {
8852 const citations : Citation [ ] = [
8953 {
@@ -100,7 +64,7 @@ describe('replaceTextWithCitations', () => {
10064 } ) ;
10165} ) ;
10266
103- describe ( 'fixCitationsLeadingMarkdown ' , ( ) => {
67+ describe ( 'fixInlineCitationsForMarkdown ' , ( ) => {
10468 test ( 'should fix leading markdown citations breaking markdown' , ( ) => {
10569 const citations : Citation [ ] = [
10670 {
@@ -142,7 +106,7 @@ describe('fixCitationsLeadingMarkdown', () => {
142106 ] ;
143107 const text =
144108 'The `ENVIRONMENT DIVISION` should be included after the `IDENTIFICATION DIVISION`. The `CONFIGURATION SECTION` and `SPECIAL-NAMES` should be included within the `ENVIRONMENT DIVISION`.' ;
145- const result = fixCitationsLeadingMarkdown ( citations , text ) ;
109+ const result = fixInlineCitationsForMarkdown ( citations , text ) ;
146110
147111 expect ( result ) . toStrictEqual ( [
148112 {
@@ -183,4 +147,122 @@ describe('fixCitationsLeadingMarkdown', () => {
183147 } ,
184148 ] ) ;
185149 } ) ;
150+
151+ test ( 'should push markdown downloable links to the end' , ( ) => {
152+ const citations : Citation [ ] = [
153+ {
154+ text : '[link](https://www.google.com)' ,
155+ start : 10 ,
156+ end : 39 ,
157+ document_ids : [ '111111' ] ,
158+ } ,
159+ {
160+ text : "[link]('.file_path.csv')" ,
161+ start : 64 ,
162+ end : 87 ,
163+ document_ids : [ '111111' ] ,
164+ } ,
165+ ] ;
166+ const text =
167+ "This is a [link](https://www.google.com) and this is downloable [link]('file_path.csv')" ;
168+ const result = fixInlineCitationsForMarkdown ( citations , text ) ;
169+ expect ( result ) . toStrictEqual ( [
170+ {
171+ text : '[link](https://www.google.com)' ,
172+ end : 39 ,
173+ start : 88 ,
174+ document_ids : [ '111111' ] ,
175+ } ,
176+ {
177+ text : "[link]('.file_path.csv')" ,
178+ end : 87 ,
179+ start : 88 ,
180+ document_ids : [ '111111' ] ,
181+ } ,
182+ ] ) ;
183+ } ) ;
184+
185+ test ( 'should remove extra blank space on markdown images' , ( ) => {
186+ const citations : Citation [ ] = [
187+ {
188+ start : 0 ,
189+ end : 25 ,
190+ text : '! [test](https://test.com)' ,
191+ document_ids : [ '12345' ] ,
192+ } ,
193+ {
194+ start : 26 ,
195+ end : 33 ,
196+ text : 'Kadabra' ,
197+ document_ids : [ '44444' ] ,
198+ } ,
199+ ] ;
200+ const text = ' Kadabra' ;
201+ const result = fixInlineCitationsForMarkdown ( citations , text ) ;
202+ expect ( result ) . toStrictEqual ( [
203+ {
204+ start : 34 ,
205+ end : 25 ,
206+ text : '' ,
207+ document_ids : [ '12345' ] ,
208+ } ,
209+ {
210+ start : 25 ,
211+ end : 32 ,
212+ text : 'Kadabra' ,
213+ document_ids : [ '44444' ] ,
214+ } ,
215+ ] ) ;
216+ } ) ;
217+ } ) ;
218+
219+ describe ( 'isReferenceBetweenSpecialTags' , ( ) => {
220+ test ( 'should return true if the citation is between <iframe> tags' , ( ) => {
221+ const matchRegex = / < i f r a m e > .* < \/ i f r a m e > / ;
222+ const text = '<iframe> This is a citation </iframe>' ;
223+ const citation : Citation = {
224+ start : 19 ,
225+ end : 27 ,
226+ text : 'citation' ,
227+ document_ids : [ '12345' ] ,
228+ } ;
229+ const result = isReferenceBetweenSpecialTags ( matchRegex , text , citation . start ) ;
230+ expect ( result ) . toBe ( true ) ;
231+ } ) ;
232+ test ( 'should return false if the citation is not between <iframe> tags' , ( ) => {
233+ const matchRegex = / < i f r a m e > .* < \/ i f r a m e > / ;
234+ const text = 'This is a citation <iframe> another test citaiton </iframe>' ;
235+ const citation : Citation = {
236+ start : 10 ,
237+ end : 18 ,
238+ text : 'citation' ,
239+ document_ids : [ '12345' ] ,
240+ } ;
241+ const result = isReferenceBetweenSpecialTags ( matchRegex , text , citation . start ) ;
242+ expect ( result ) . toBe ( false ) ;
243+ } ) ;
244+ test ( 'should return true if the citation is between ``` tags' , ( ) => {
245+ const matchRegex = / ` ` ` [ \s \S ] * ?` ` ` / g;
246+ const text = '``` This is a citation ```' ;
247+ const citation : Citation = {
248+ start : 14 ,
249+ end : 22 ,
250+ text : 'citation' ,
251+ document_ids : [ '12345' ] ,
252+ } ;
253+ const result = isReferenceBetweenSpecialTags ( matchRegex , text , citation . start ) ;
254+ expect ( result ) . toBe ( true ) ;
255+ } ) ;
256+ test ( 'should return false if the citation is not between ``` tags' , ( ) => {
257+ const matchRegex = / ` ` ` [ \s \S ] * ?` ` ` / g;
258+ const text = '``` This is a citation ``` another test citaiton' ;
259+ const citation : Citation = {
260+ start : 40 ,
261+ end : 48 ,
262+ text : 'citation' ,
263+ document_ids : [ '12345' ] ,
264+ } ;
265+ const result = isReferenceBetweenSpecialTags ( matchRegex , text , citation . start ) ;
266+ expect ( result ) . toBe ( false ) ;
267+ } ) ;
186268} ) ;
0 commit comments