|
@@ -18,37 +18,58 @@ import TocAndAnchorConfigurer from './markdown-it/toc-and-anchor';
|
|
|
|
|
|
|
|
export default class GrowiRenderer {
|
|
export default class GrowiRenderer {
|
|
|
|
|
|
|
|
-
|
|
|
|
|
- constructor(crowi, option) {
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param {Crowi} crowi
|
|
|
|
|
+ * @param {GrowiRenderer} originRenderer may be customized by plugins
|
|
|
|
|
+ * @param {object} options
|
|
|
|
|
+ */
|
|
|
|
|
+ constructor(crowi, originRenderer, options) {
|
|
|
this.crowi = crowi;
|
|
this.crowi = crowi;
|
|
|
-
|
|
|
|
|
- this.preProcessors = [
|
|
|
|
|
|
|
+ this.originRenderer = originRenderer || {};
|
|
|
|
|
+ this.options = Object.assign( // merge options
|
|
|
|
|
+ { isAutoSetup: true }, // default options
|
|
|
|
|
+ options || {}); // specified options
|
|
|
|
|
+
|
|
|
|
|
+ // initialize processors
|
|
|
|
|
+ // that will be retrieved if originRenderer exists
|
|
|
|
|
+ this.preProcessors = this.originRenderer.preProcessors || [
|
|
|
new Linker(crowi),
|
|
new Linker(crowi),
|
|
|
new CsvToTable(crowi),
|
|
new CsvToTable(crowi),
|
|
|
new XssFilter(crowi),
|
|
new XssFilter(crowi),
|
|
|
];
|
|
];
|
|
|
- this.postProcessors = [
|
|
|
|
|
|
|
+ this.postProcessors = this.originRenderer.postProcessors || [
|
|
|
];
|
|
];
|
|
|
|
|
|
|
|
- this.langProcessors = {
|
|
|
|
|
|
|
+ this.langProcessors = this.originRenderer.langProcessors || {
|
|
|
'template': new Template(crowi),
|
|
'template': new Template(crowi),
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- this.configure = this.configure.bind(this);
|
|
|
|
|
- this.configureMarkdownIt = this.configureMarkdownIt.bind(this);
|
|
|
|
|
|
|
+ this.initMarkdownItConfigurers = this.initMarkdownItConfigurers.bind(this);
|
|
|
|
|
+ this.setup = this.setup.bind(this);
|
|
|
this.process = this.process.bind(this);
|
|
this.process = this.process.bind(this);
|
|
|
this.codeRenderer = this.codeRenderer.bind(this);
|
|
this.codeRenderer = this.codeRenderer.bind(this);
|
|
|
|
|
|
|
|
- this.md = new MarkdownIt();
|
|
|
|
|
- this.configure(this.crowi.getConfig());
|
|
|
|
|
- this.configureMarkdownIt(option);
|
|
|
|
|
|
|
+ // init markdown-it
|
|
|
|
|
+ this.md = new MarkdownIt({
|
|
|
|
|
+ html: true,
|
|
|
|
|
+ linkify: true,
|
|
|
|
|
+ highlight: this.codeRenderer,
|
|
|
|
|
+ });
|
|
|
|
|
+ this.initMarkdownItConfigurers(options);
|
|
|
|
|
|
|
|
|
|
+ // auto setup
|
|
|
|
|
+ if (this.options.isAutoSetup) {
|
|
|
|
|
+ this.setup(crowi.getConfig());
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- configureMarkdownIt(option) {
|
|
|
|
|
|
|
+ initMarkdownItConfigurers(options) {
|
|
|
const crowi = this.crowi;
|
|
const crowi = this.crowi;
|
|
|
|
|
|
|
|
- let configurers = [
|
|
|
|
|
|
|
+ this.isMarkdownItConfigured = false;
|
|
|
|
|
+
|
|
|
|
|
+ this.markdownItConfigurers = [
|
|
|
new CommonPluginsConfigurer(crowi),
|
|
new CommonPluginsConfigurer(crowi),
|
|
|
new HeaderConfigurer(crowi),
|
|
new HeaderConfigurer(crowi),
|
|
|
new TableConfigurer(crowi),
|
|
new TableConfigurer(crowi),
|
|
@@ -57,43 +78,41 @@ export default class GrowiRenderer {
|
|
|
new PlantUMLConfigurer(crowi),
|
|
new PlantUMLConfigurer(crowi),
|
|
|
];
|
|
];
|
|
|
|
|
|
|
|
- if (option != null) {
|
|
|
|
|
- const mode = option.mode;
|
|
|
|
|
- switch (mode) {
|
|
|
|
|
- case 'page':
|
|
|
|
|
- configurers = configurers.concat([
|
|
|
|
|
- new TocAndAnchorConfigurer(crowi, option.renderToc),
|
|
|
|
|
- new HeaderLineNumberConfigurer(crowi),
|
|
|
|
|
- ]);
|
|
|
|
|
- break;
|
|
|
|
|
- case 'editor':
|
|
|
|
|
- configurers = configurers.concat([
|
|
|
|
|
- new HeaderLineNumberConfigurer(crowi)
|
|
|
|
|
- ]);
|
|
|
|
|
- break;
|
|
|
|
|
- case 'timeline':
|
|
|
|
|
- break;
|
|
|
|
|
- case 'searchresult':
|
|
|
|
|
- break;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ // add configurers according to mode
|
|
|
|
|
+ const mode = options.mode;
|
|
|
|
|
+ switch (mode) {
|
|
|
|
|
+ case 'page':
|
|
|
|
|
+ this.markdownItConfigurers = this.markdownItConfigurers.concat([
|
|
|
|
|
+ new TocAndAnchorConfigurer(crowi, options.renderToc),
|
|
|
|
|
+ new HeaderLineNumberConfigurer(crowi),
|
|
|
|
|
+ ]);
|
|
|
|
|
+ break;
|
|
|
|
|
+ case 'editor':
|
|
|
|
|
+ this.markdownItConfigurers = this.markdownItConfigurers.concat([
|
|
|
|
|
+ new HeaderLineNumberConfigurer(crowi)
|
|
|
|
|
+ ]);
|
|
|
|
|
+ break;
|
|
|
|
|
+ case 'timeline':
|
|
|
|
|
+ break;
|
|
|
|
|
+ case 'searchresult':
|
|
|
|
|
+ break;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- configurers.forEach((configurer) => {
|
|
|
|
|
- configurer.configure(this.md);
|
|
|
|
|
- });
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * configure with crowi config
|
|
|
|
|
- * @param {any} config
|
|
|
|
|
|
|
+ * setup with crowi config
|
|
|
|
|
+ * @param {any} config crowi config
|
|
|
*/
|
|
*/
|
|
|
- configure(config) {
|
|
|
|
|
|
|
+ setup(config) {
|
|
|
this.md.set({
|
|
this.md.set({
|
|
|
- html: true,
|
|
|
|
|
- linkify: true,
|
|
|
|
|
breaks: config.isEnabledLineBreaks,
|
|
breaks: config.isEnabledLineBreaks,
|
|
|
- highlight: this.codeRenderer,
|
|
|
|
|
});
|
|
});
|
|
|
|
|
+
|
|
|
|
|
+ if (!this.isMarkdownItConfigured) {
|
|
|
|
|
+ this.markdownItConfigurers.forEach((configurer) => {
|
|
|
|
|
+ configurer.configure(this.md);
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
preProcess(markdown) {
|
|
preProcess(markdown) {
|
|
@@ -103,6 +122,7 @@ export default class GrowiRenderer {
|
|
|
}
|
|
}
|
|
|
markdown = this.preProcessors[i].process(markdown);
|
|
markdown = this.preProcessors[i].process(markdown);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
return markdown;
|
|
return markdown;
|
|
|
}
|
|
}
|
|
|
|
|
|