-
Notifications
You must be signed in to change notification settings - Fork 8
Customize the rendering of inline references #27
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
Open
xUser5000
wants to merge
11
commits into
main
Choose a base branch
from
inline-references-rendering
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b1965e9
refactor(Bibliography Renderer): Abstract collecting references into …
xUser5000 85cdd8f
feat(Bibliography Renderer): Search for words that starts with @
xUser5000 dbab3be
feat(Bibliography Renderer): Extract all cite keys from the note body
xUser5000 45138b4
perf(Bibliography Renderer): Slight performance improvements
xUser5000 c2a1de7
fix(Bibliography Renderer): Reference not found
xUser5000 35ff70b
refactor(Bibliography Renderer): Distinguish between message types
xUser5000 9e353e6
feat(Bibliography Renderer): Replace inline references with a fixed text
xUser5000 c23d924
feat(Bibliography Renderer): Transform references into an "inline_ref…
xUser5000 499be3e
feat(Bibliography Renderer): Replace custom inline references with a …
xUser5000 b802a2a
feat(Bibliography Renderer): Render all references that matches a cer…
xUser5000 bfb9e20
feat(Bibliography Renderer): Render two patterns
xUser5000 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,2 @@ | ||
| export const FORMAT_REFERENCES: string = "format_references"; | ||
| export const GET_REFERENCE_BY_ID: string = "get_reference_by_id"; |
This file contains hidden or 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,51 @@ | ||
| /** | ||
| * Given a list of markdown tokens and their children, | ||
| * Replaces every text token that contains a reference with an "inline_reference" token | ||
| * and returns a list of reference IDs that exists in the markdown tree | ||
| * Uses recursive Depth-First-Search | ||
| */ | ||
| export function extractReferences(tokens: any[], Token: any): string[] { | ||
| const ids: string[] = []; | ||
| const referencePattern = /@(\w|:|\?|\-)+/g; | ||
|
|
||
| /* Collect all words that matches the regular expression */ | ||
| DFS(tokens, Token); | ||
|
|
||
| function DFS(nodes: any[], Token: any): void { | ||
| for (let i = 0; i < nodes.length; i++) { | ||
| if (nodes[i].type === "text") { | ||
| // If it's the default format, keep it as it is | ||
| // Example: [@dijkstraNoteTwoProblems1959](https://scholar.google.com/scholar?hl=en&as_sdt=0%2C5&q=two+problems+connexion+with+graphs&btnG=&oq=note+on+two+problems+in+connexion+) | ||
| if ( | ||
| nodes[i].content.startsWith("@") && | ||
| i - 1 >= 0 && | ||
| nodes[i - 1].type === "link_open" && | ||
| i + 1 < nodes.length && | ||
| nodes[i + 1].type === "link_close" | ||
| ) { | ||
| ids.push(nodes[i].content.substring(1)); | ||
| continue; | ||
| } | ||
|
|
||
| const matches: string[] = | ||
| nodes[i].content.match(referencePattern); | ||
| if (!matches || !matches.length) continue; | ||
|
|
||
| // Replace the current textToken with a reference Token with the same content | ||
| // Just to be parsed later on by the renderer | ||
| const content = nodes[i].content; | ||
| nodes[i] = new Token("inline_reference", "", 0); | ||
| nodes[i].content = content; | ||
|
|
||
| // push all the matches to the ids array, without "@" | ||
| matches.forEach((match) => ids.push(match.substring(1))); | ||
| } | ||
| // Recursively search child nodes | ||
| else if (nodes[i].children && nodes[i].children.length) { | ||
| DFS(nodes[i].children, Token); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return ids; | ||
| } |
This file contains hidden or 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,85 @@ | ||
| import { GET_REFERENCE_BY_ID } from "./Message"; | ||
| import { patterns } from "./reference-patterns"; | ||
|
|
||
| /** | ||
| * Given a list of tokens and their children, | ||
| * return a string of javascript code that requests the reference data | ||
| */ | ||
| export function generateScript(tokens: any[], contentScriptId: string): string { | ||
| let js: string = ""; | ||
|
|
||
| /* Collect all words that matches the regular expression */ | ||
| let inlineReferenceCounter: number = 0; | ||
| DFS(tokens, contentScriptId); | ||
|
|
||
| function DFS(nodes: any[], contentScriptId: string): void { | ||
| for (let i = 0; i < nodes.length; i++) { | ||
| if (nodes[i].type === "inline_reference") { | ||
| let matches: string[]; | ||
|
|
||
| matches = nodes[i].content.match(patterns[0]); | ||
| if (matches && matches.length) { | ||
| for (let j = 0; j < matches.length; j++) { | ||
| const match: string = matches[j]; | ||
| const refId: string = match.substring( | ||
| 3, | ||
| match.length - 1 | ||
| ); | ||
| const viewId = `bibtex_reference_${inlineReferenceCounter}`; | ||
| const script: string = ` | ||
| webviewApi.postMessage("${contentScriptId}", ${JSON.stringify( | ||
| { | ||
| type: GET_REFERENCE_BY_ID, | ||
| id: refId, | ||
| } | ||
| )}).then(ref => { | ||
| if (ref && ref.year) { | ||
| const view = document.getElementById("${viewId}"); | ||
| view.textContent = "(" + ref.year + ")"; | ||
| } | ||
| }); | ||
| `; | ||
|
|
||
| js += script; | ||
| inlineReferenceCounter++; | ||
| } | ||
| } | ||
|
|
||
| matches = nodes[i].content.match(patterns[1]); | ||
| if (matches && matches.length) { | ||
| for (let j = 0; j < matches.length; j++) { | ||
| const match: string = matches[j]; | ||
| const refId: string = match.substring( | ||
| 2, | ||
| match.length - 1 | ||
| ); | ||
| const viewId = `bibtex_reference_${inlineReferenceCounter}`; | ||
| const script: string = ` | ||
| webviewApi.postMessage("${contentScriptId}", ${JSON.stringify( | ||
| { | ||
| type: GET_REFERENCE_BY_ID, | ||
| id: refId, | ||
| } | ||
| )}).then(ref => { | ||
| if (ref && ref.year && ref.auth) { | ||
| const view = document.getElementById("${viewId}"); | ||
| view.textContent = "(" + ref.auth + " " + ref.year + ")"; | ||
| } | ||
| }); | ||
| `; | ||
|
|
||
| js += script; | ||
| inlineReferenceCounter++; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Recursively search child nodes | ||
| else if (nodes[i].children && nodes[i].children.length) { | ||
| DFS(nodes[i].children, contentScriptId); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return js; | ||
| } |
This file contains hidden or 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 hidden or 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,5 @@ | ||
| export const patterns: RegExp[] = [ | ||
| /\[-@(\w|:|\?|\-)+\]/g, | ||
| /\[@(\w|:|\?|\-)+\]/g, | ||
| /@(\w|:|\?|\-)+/g, | ||
| ]; |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That shouldn't be "any" especially since you have defined a type for the references.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see