Skip to content
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

feat: add html sanitizing [DOCSTOOLS-1350] #177

Merged
merged 4 commits into from
Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
dist
lib
99 changes: 97 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"prepublishOnly": "npm run lint && npm run test && npm run build"
},
"dependencies": {
"@types/sanitize-html": "^2.6.2",
"chalk": "4.1.2",
"get-root-node-polyfill": "1.0.0",
"github-slugger": "1.4.0",
Expand All @@ -42,6 +43,7 @@
"markdownlint": "^0.25.1",
"markdownlint-rule-helpers": "0.17.2",
"postcss": "8.4.16",
"sanitize-html": "^2.7.1",
"slugify": "1.6.5"
},
"devDependencies": {
Expand Down
13 changes: 11 additions & 2 deletions src/transform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import makeHighlight from './highlight';
import extractTitle from './title';
import getHeadings from './headings';
import liquid from './liquid';
import sanitizeHtml, {SanitizeOptions} from './sanitize';

import notes from './plugins/notes';
import anchors from './plugins/anchors';
Expand All @@ -24,7 +25,7 @@ import monospace from './plugins/monospace';
import yfmTable from './plugins/table';
import {initMd} from './md';
import {MarkdownItPluginCb} from './plugins/typings';
import {HighlightLangMap, Heading} from './typings';
import type {HighlightLangMap, Heading} from './typings';

interface OutputType {
result: {
Expand All @@ -49,6 +50,8 @@ interface OptionsType {
leftDelimiter?: string;
rightDelimiter?: string;
isLiquided?: boolean;
needToSanitizeHtml?: boolean;
sanitizeOptions?: SanitizeOptions;
needFlatListHeadings?: boolean;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
plugins?: MarkdownItPluginCb<any>[];
Expand All @@ -67,6 +70,8 @@ function transform(originInput: string, opts: OptionsType = {}): OutputType {
linkify = false,
breaks = true,
conditionsInCode = false,
needToSanitizeHtml = false,
sanitizeOptions,
needFlatListHeadings = false,
disableLiquid = false,
leftDelimiter = '{',
Expand Down Expand Up @@ -137,7 +142,11 @@ function transform(originInput: string, opts: OptionsType = {}): OutputType {

// add all term template tokens to the end of the html
const termTokens = (env.termTokens as Token[]) || [];
const html = md.renderer.render([...tokens, ...termTokens], md.options, env);
let html = md.renderer.render([...tokens, ...termTokens], md.options, env);
if (needToSanitizeHtml) {
html = sanitizeHtml(html, sanitizeOptions);
}

const assets = md.assets;
const meta = md.meta;

Expand Down
Loading