generated from idea2app/Next-Bootstrap-ts
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[optimize] simplify models & middlewares with Next-SSR-middleware 0.8…
… & MobX-GitHub 0.3 [optimize] TurnDown rules & JSON Edtior updating
- Loading branch information
Showing
20 changed files
with
144 additions
and
434 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,67 @@ | ||
import TurnDown from 'turndown'; | ||
// @ts-ignore | ||
import { gfm } from 'turndown-plugin-gfm'; | ||
import { gfm, strikethrough, tables, taskListItems } from 'turndown-plugin-gfm'; | ||
|
||
const Empty_HREF = /^(#|javascript:\s*void\(0\);?\s*)$/; | ||
|
||
type TurnDownGFM = (td: TurnDown) => void; | ||
export default new TurnDown({ | ||
headingStyle: 'atx', | ||
hr: '---', | ||
bulletListMarker: '-', | ||
codeBlockStyle: 'fenced', | ||
linkStyle: 'referenced', | ||
}) | ||
.use(strikethrough) | ||
.use(tables) | ||
.use(taskListItems) | ||
.use(gfm) | ||
.addRule('non_url', { | ||
filter: node => | ||
['a', 'area'].includes(node.nodeName.toLowerCase()) && | ||
Empty_HREF.test(node.getAttribute('href') || ''), | ||
replacement: (content, node) => | ||
content.trim() || (node instanceof HTMLElement ? node.title.trim() : ''), | ||
}) | ||
.addRule('img-srcset', { | ||
filter: ['img'], | ||
replacement(_, node) { | ||
const { alt, title, src, srcset } = node as HTMLImageElement; | ||
const [firstSet] = srcset.split(',')[0]?.split(/\s+/) || []; | ||
|
||
export default class extends TurnDown { | ||
constructor(options?: any) { | ||
super({ | ||
headingStyle: 'atx', | ||
hr: '---', | ||
bulletListMarker: '-', | ||
codeBlockStyle: 'fenced', | ||
linkStyle: 'referenced', | ||
...options, | ||
}); | ||
const content = [src || firstSet, title && JSON.stringify(title)].filter( | ||
Boolean, | ||
); | ||
return `![${alt}](${content.join(' ')})`; | ||
}, | ||
}) | ||
.addRule('source-srcset', { | ||
filter: ['picture'], | ||
replacement(_, node) { | ||
const { src, alt, title } = node.querySelector('img') || {}; | ||
|
||
this.use(gfm as TurnDownGFM) | ||
.addRule('non_url', { | ||
filter: node => | ||
['a', 'area'].includes(node.nodeName.toLowerCase()) && | ||
Empty_HREF.test(node.getAttribute('href') || ''), | ||
replacement: (content, node) => | ||
content.trim() || | ||
(node instanceof HTMLElement ? node.title.trim() : ''), | ||
}) | ||
.addRule('asset_code', { | ||
filter: ['style', 'script'], | ||
replacement: () => '', | ||
}); | ||
} | ||
} | ||
const sourceList = Array.from( | ||
node.querySelectorAll('source'), | ||
({ sizes, srcset }) => { | ||
const size = Math.max( | ||
...sizes | ||
.split(/,|\)/) | ||
.map(pixel => parseFloat(pixel.trim())) | ||
.filter(Boolean), | ||
); | ||
const [src] = srcset.split(',')[0]?.split(/\s+/) || []; | ||
|
||
return { size, src }; | ||
}, | ||
); | ||
const sources = sourceList.sort(({ size: a }, { size: b }) => b - a); | ||
|
||
const content = [ | ||
src || sources[0]?.src, | ||
title && JSON.stringify(title), | ||
].filter(Boolean); | ||
|
||
return `![${alt}](${content.join(' ')})`; | ||
}, | ||
}) | ||
.remove(node => node.matches('style, script, aside, form, [class*="ads" i]')) | ||
.keep('iframe'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
9dc6481
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Deploy preview for oss-toolbox ready!
✅ Preview
https://oss-toolbox-12f3pfplz-techquerys-projects.vercel.app
Built with commit 9dc6481.
This pull request is being automatically deployed with vercel-action