Skip to content

Commit

Permalink
Merge pull request rstudio#8523 from rstudio/bugfixes/cite-in-cite-redux
Browse files Browse the repository at this point in the history
Bugfixes/cite in cite redux
  • Loading branch information
jjallaire authored Dec 3, 2020
2 parents 1f34761 + 68de6c4 commit 6625189
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/gwt/panmirror/src/editor/src/api/bibtex/bibtex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ const toBibtex = (entries: Entry[]): string => {
bibTexStr = bibTexStr + `@${entry.type}{${entry.key}`;

// The fields for this item
if (entry.values) {
if (entry.values && Object.keys(entry.values).length > 0) {
sortedKeys(entry.values).forEach(key => {
if (entry.values) {
const rawValue = entry.values[key];
Expand All @@ -163,6 +163,10 @@ const toBibtex = (entries: Entry[]): string => {
bibTexStr = bibTexStr + `,\n\t${key} = ${value}`;
}
});
} else {
// There are no values, we need to minimally place a ',' at the end of the id
// If we omit this, pandoc cannot parse the bibliography
bibTexStr = bibTexStr + ",";
}

// Close the entry
Expand Down
20 changes: 18 additions & 2 deletions src/gwt/panmirror/src/editor/src/marks/cite/cite-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { BibliographyManager } from '../../api/bibliography/bibliography';

import { ensureSourcesInBibliography } from './cite';
import { showInsertCitationDialog, InsertCitationDialogResult } from '../../behaviors/insert_citation/insert_citation';
import { markIsActive } from '../../api/mark';
import { markIsActive, getMarkRange } from '../../api/mark';

export class InsertCitationCommand extends ProsemirrorCommand {
private initialSelectionKey: string | undefined;
Expand Down Expand Up @@ -92,13 +92,22 @@ export class InsertCitationCommand extends ProsemirrorCommand {
// always perform a 'note' style citation insert
// If we're already inside a cite including [], don't bother inserting wrapper
if (!alreadyInCite && includeWrapper) {
const wrapperText = schema.text(`[]`, []);
const wrapperText = schema.text('[]');
tr.insert(tr.selection.from, wrapperText);

// move the selection into the wrapper
setTextSelection(tr.selection.from - 1)(tr);
}

// If the previous character is a part of a cite_id, advance to the end of the mark,
// insert a separator, and then proceed
const preCiteIdRange = getMarkRange(tr.doc.resolve(start - 1), schema.marks.cite_id);
if (preCiteIdRange) {
setTextSelection(preCiteIdRange.to)(tr);
const separator = schema.text('; ');
tr.insert(tr.selection.from, separator);
}

// insert the CiteId marks and text
bibliographySources.forEach((citation, i) => {
const citeIdMark = schema.marks.cite_id.create();
Expand All @@ -109,6 +118,13 @@ export class InsertCitationCommand extends ProsemirrorCommand {
}
});

// If the next character is a part of a cite_id, insert a separator (that will appear after the current citeId)
const postCiteIdRange = getMarkRange(tr.doc.resolve(tr.selection.from + 1), schema.marks.cite_id);
if (postCiteIdRange) {
const separator = schema.text('; ');
tr.insert(tr.selection.from, separator);
}

// Enclose wrapper in the cite mark (if not already in a cite)
if (!alreadyInCite) {
const endOfWrapper = includeWrapper ? tr.selection.from + 1 : tr.selection.from;
Expand Down

0 comments on commit 6625189

Please sign in to comment.