-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
90 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import extractCitations from './citeExtractor'; | ||
import extractParaphs from './paraphExtractor'; | ||
|
||
/** | ||
* | ||
* @param {string} markdown | ||
* @returns {any[]} | ||
*/ | ||
|
||
export default function quoteIdsWithContexts(markdown) { | ||
let quotes = {}; | ||
|
||
extractParaphs(markdown).forEach((paraph) => { | ||
extractCitations(paraph).forEach((result) => { | ||
result.citations.forEach(({ id }) => { | ||
if (quotes[id]) { | ||
quotes[id].contexts.add(paraph); | ||
} else { | ||
quotes[id] = { | ||
contexts: new Set([paraph]), | ||
id, | ||
}; | ||
} | ||
}); | ||
}); | ||
}); | ||
|
||
return Object.values(quotes).map((quote) => ({ | ||
...quote, | ||
contexts: Array.from(quote.contexts), | ||
})); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import quotesWithContexts from './quotesWithContexts'; | ||
|
||
describe('quotesWithContexts', () => { | ||
it('should parse one quote per paragraph', () => { | ||
const text = `First paragraph quoting @smith04. | ||
Second paragraph quoting @doe99.`; | ||
|
||
const result = quotesWithContexts(text); | ||
|
||
expect(result).toEqual([ | ||
expect.objectContaining({ | ||
contexts: ['First paragraph quoting @smith04.'], | ||
id: 'smith04', | ||
}), | ||
expect.objectContaining({ | ||
contexts: ['Second paragraph quoting @doe99.'], | ||
id: 'doe99', | ||
}), | ||
]); | ||
}); | ||
|
||
it('should parse many quotes per paragraph', () => { | ||
const text = `First paragraph quoting @smith04. | ||
Second paragraph quoting @smith04 and @doe99. @doe99 is quoted once.`; | ||
|
||
const result = quotesWithContexts(text); | ||
|
||
expect(result).toEqual([ | ||
expect.objectContaining({ | ||
contexts: [ | ||
'First paragraph quoting @smith04.', | ||
'Second paragraph quoting @smith04 and @doe99. @doe99 is quoted once.', | ||
], | ||
id: 'smith04', | ||
}), | ||
expect.objectContaining({ | ||
contexts: ['Second paragraph quoting @smith04 and @doe99. @doe99 is quoted once.'], | ||
id: 'doe99', | ||
}), | ||
]); | ||
}); | ||
}); |