Skip to content

Commit

Permalink
fixed autonumbering for rendered markdown doc
Browse files Browse the repository at this point in the history
  • Loading branch information
zuoyuanh committed Aug 25, 2018
1 parent e755f1c commit 475b7dd
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 159 deletions.
8 changes: 8 additions & 0 deletions build-dev.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
npm i
npm run build
cd ../jupyterlab
jlpm run remove:package jupyterlab-toc
jlpm run add:sibling ../jupyterlab-toc
jlpm run build
cd ../jupyterlab-celltags

13 changes: 9 additions & 4 deletions src/generators/markdowndocgenerator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,13 @@ export function createRenderedMarkdownGenerator(
sanitizer: ISanitizer,
widget: TableOfContents
): TableOfContentsRegistry.IGenerator<MimeDocument> {
const options = new MarkdownDocGeneratorOptionsManager(widget, {
needNumbering: true
});
const options = new MarkdownDocGeneratorOptionsManager(
widget,
{
needNumbering: true
},
tracker
);
return {
tracker,
usesLatex: true,
Expand All @@ -113,7 +117,8 @@ export function createRenderedMarkdownGenerator(
widget.content.node,
onClickFactory,
sanitizer,
numberingDict
numberingDict,
options.numbering
);
}
};
Expand Down
13 changes: 12 additions & 1 deletion src/generators/markdowndocgenerator/itemrenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ export function markdownDocItemRenderer(
// Render numbering if needed
let numbering = item.numbering && options.numbering ? item.numbering : '';
fontSize = levelsSizes[item.level] + 'px';
let jsx = <span style={{ fontSize }}> {numbering + item.text}</span>;
let jsx;
if (item.html) {
jsx = (
<span
dangerouslySetInnerHTML={{ __html: numbering + item.html }}
className={item.type + '-cell'}
style={{ fontSize }}
/>
);
} else {
jsx = <span style={{ fontSize }}> {numbering + item.text}</span>;
}
return jsx;
}
32 changes: 31 additions & 1 deletion src/generators/markdowndocgenerator/optionsmanager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,45 @@ import { TableOfContentsRegistry } from '../../registry';

import { TableOfContents } from '../../toc';

import { IInstanceTracker } from '@jupyterlab/apputils';

import { MimeDocument } from '@jupyterlab/docregistry';

import { each } from '@phosphor/algorithm';

export class MarkdownDocGeneratorOptionsManager extends TableOfContentsRegistry.IGeneratorOptionsManager {
constructor(widget: TableOfContents, options: { needNumbering: boolean }) {
constructor(
widget: TableOfContents,
options: { needNumbering: boolean },
tracker?: IInstanceTracker<MimeDocument>
) {
super();
this._numbering = options.needNumbering;
this._tracker = tracker;
this._widget = widget;
}

// Show/hide numbering in the document
private changeNumberingStateDocument(showNumbering: boolean) {
if (this._tracker && this._tracker.currentWidget) {
let numberingNodes = this._tracker.currentWidget.content.node.querySelectorAll(
'.numbering-entry'
);
each(numberingNodes, numbering => {
if (!showNumbering) {
numbering.setAttribute('hidden', 'true');
} else {
numbering.removeAttribute('hidden');
}
});
}
}

set numbering(value: boolean) {
this._numbering = value;
this._widget.update();
// this.notebookMetadata = ['toc-autonumbering', this._numbering];
this.changeNumberingStateDocument(this._numbering);
}

get numbering() {
Expand All @@ -25,5 +54,6 @@ export class MarkdownDocGeneratorOptionsManager extends TableOfContentsRegistry.
}

private _numbering: boolean;
private _tracker?: IInstanceTracker<MimeDocument>;
private _widget: TableOfContents;
}
Loading

0 comments on commit 475b7dd

Please sign in to comment.