Skip to content

Commit

Permalink
fix: Expose 'includes' and 'media' options (regression) (#264)
Browse files Browse the repository at this point in the history
  • Loading branch information
tgreyuk committed Nov 12, 2021
1 parent 07d1174 commit 435e0d2
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
44 changes: 42 additions & 2 deletions packages/typedoc-plugin-markdown/src/resources/helpers/comment.ts
@@ -1,14 +1,18 @@
import * as Handlebars from 'handlebars';
import { MarkdownTheme } from '../../theme';
import * as path from 'path';
import * as fs from 'fs';

const URL_PREFIX = /^(http|ftp)s?:\/\//;
const BRACKETS = /\[\[([^\]]+)\]\]/g;
const INLINE_TAG =
/(?:\[(.+?)\])?\{@(link|linkcode|linkplain)\s+((?:.|\n)+?)\}/gi;
const INCLUDE_PATTERN = /\[\[include:([^\]]+?)\]\]/g;
const MEDIA_PATTERN = /media:\/\/([^ "\)\]\}]+)/g;

export default function (theme: MarkdownTheme) {
Handlebars.registerHelper('comment', function (this: string) {
const { project } = theme;
const { project, includes, mediaDirectory } = theme;

function replaceBrackets(text: string) {
return text.replace(
Expand Down Expand Up @@ -79,6 +83,42 @@ export default function (theme: MarkdownTheme) {
}
}

return replaceInlineTags(replaceBrackets(this));
let text = this;
const context = Object.assign(text, '');

if (includes) {
text = text.replace(
INCLUDE_PATTERN,
(match: string, includesPath: string) => {
includesPath = path.join(includes!, includesPath.trim());
if (
fs.existsSync(includesPath) &&
fs.statSync(includesPath).isFile()
) {
const contents = fs.readFileSync(includesPath, 'utf-8');
if (includesPath.substr(-4).toLocaleLowerCase() === '.hbs') {
const template = Handlebars.compile(contents);
return template(context);
} else {
return contents;
}
} else {
return '';
}
},
);
}

if (mediaDirectory) {
text = text.replace(MEDIA_PATTERN, (match: string, mediaPath: string) => {
if (fs.existsSync(path.join(mediaDirectory!, mediaPath))) {
return Handlebars.helpers.relativeURL('media') + '/' + mediaPath;
} else {
return match;
}
});
}

return replaceInlineTags(replaceBrackets(text));
});
}
6 changes: 6 additions & 0 deletions packages/typedoc-plugin-markdown/src/theme.ts
Expand Up @@ -62,6 +62,12 @@ export class MarkdownTheme extends Theme {
@BindOption('publicPath')
publicPath!: string;

@BindOption('includes')
includes!: string;

@BindOption('media')
mediaDirectory!: string;

static URL_PREFIX = /^(http|ftp)s?:\/\//;

project?: ProjectReflection;
Expand Down
Expand Up @@ -35,11 +35,13 @@ anotherFunction()
exports[`Comments: should convert comments with includes' 1`] = `
"This is an example of include
[[include:class-example.md]]
This is a simple example on how to use include.
![My image alt text](../media/logo.png)
This is an example of handlebars include
[[include:class-example.hbs]]
This is a simple example on a handlebars file.
"
`;

Expand Down

0 comments on commit 435e0d2

Please sign in to comment.