Skip to content

Commit

Permalink
feat: add target="_blank" for statements
Browse files Browse the repository at this point in the history
  • Loading branch information
ouuan committed Feb 17, 2023
1 parent 3f623fe commit d1a17cc
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"@types/glob": "^7.2.0",
"@types/jsdom": "^16.2.14",
"@types/markdown-it": "^12.2.3",
"@types/markdown-it-link-attributes": "^3.0.1",
"@typescript-eslint/eslint-plugin": "^5.30.5",
"@typescript-eslint/parser": "^5.30.5",
"@vitejs/plugin-vue": "^2.3.3",
Expand All @@ -58,6 +59,7 @@
"glob-promise": "^4.2.2",
"jsdom": "^20.0.0",
"markdown-it": "^13.0.1",
"markdown-it-link-attributes": "^4.0.1",
"rollup": "^2.75.7",
"rollup-plugin-analyzer": "^4.0.0",
"sass": "^1.53.0",
Expand Down
14 changes: 14 additions & 0 deletions pnpm-lock.yaml

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

2 changes: 1 addition & 1 deletion src/utils/getPuzzle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export async function fetchStatement(id: number) {
throw new Error(response.statusText);
}
const text = await response.text();
return DOMPurify.sanitize(text);
return DOMPurify.sanitize(text, { ADD_ATTR: ['target'] });
}

export async function fetchTargetCodeEncoded(id: number) {
Expand Down
9 changes: 8 additions & 1 deletion src/vite/transformPuzzles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { Plugin } from 'vite';
import { JSDOM } from 'jsdom';
import createDOMPurify from 'dompurify';
import MarkdownIt from 'markdown-it';
import mila from 'markdown-it-link-attributes';
import {
beginTimestamp,
host,
Expand All @@ -23,6 +24,12 @@ import {
const { window } = new JSDOM('');
const { sanitize } = createDOMPurify(window as any as Window);
const md = new MarkdownIt('commonmark');
md.use(mila, {
attrs: {
target: '_blank',
rel: 'noopener noreferrer',
},
});

async function clear() {
await Promise.all(['statement', 'targetCode'].map(async (dir) => {
Expand Down Expand Up @@ -58,7 +65,7 @@ async function transformStatements(items: FeedItem[]) {
return Promise.all(matches.map(async (path) => {
const buffer = await readFile(path);
const html = md.render(buffer.toString());
const content = sanitize(html);
const content = sanitize(html, { ADD_ATTR: ['target'] });
const base = basename(path, '.md');
const date = dateForBase(base);
if (!datePublished(date)) return;
Expand Down

0 comments on commit d1a17cc

Please sign in to comment.