-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 將 README.md 寫入 plugin.xml -> description
- Loading branch information
1 parent
d3eb8ad
commit be62451
Showing
4 changed files
with
267 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import MarkdownIt from 'markdown-it'; | ||
import { outputFile, readFile } from 'fs-extra'; | ||
import { join } from 'upath2'; | ||
import { __root } from '../test/__root'; | ||
import { readFileSync } from 'fs'; | ||
import Token from 'markdown-it/lib/token'; | ||
import { LazyURL } from 'lazy-url'; | ||
|
||
const baseUrl = 'https://raw.githubusercontent.com/bluelovers/idea-l10n-zht/master/'; | ||
|
||
const baseUrl2 = 'https://github.com/bluelovers/idea-l10n-zht/blob/master/'; | ||
|
||
export function createMarkdownIt() | ||
{ | ||
const md = new MarkdownIt({ | ||
html: true, | ||
linkify: true, | ||
breaks: true, | ||
xhtmlOut: true, | ||
typographer: true, | ||
}); | ||
|
||
md.linkify.set({ | ||
fuzzyLink: false, | ||
}); | ||
|
||
md.core.ruler.push('baseurl', (state) => | ||
{ | ||
_rewriteTokens(state.tokens); | ||
}); | ||
|
||
return md | ||
} | ||
|
||
export function renderMarkdown(input: string | Uint8Array) | ||
{ | ||
const md = createMarkdownIt(); | ||
|
||
return `<meta charset="utf-8"/>` + md.render(input?.toString()) | ||
} | ||
|
||
export function _rewriteTokens(tokens: Token[]): void | ||
{ | ||
for (const token of tokens) | ||
{ | ||
if (token.type === 'image') | ||
{ | ||
for (const attr of token.attrs) | ||
{ | ||
if (attr[0] === 'src') | ||
{ | ||
//attr[1] = baseUrl + attr[1]; | ||
attr[1] = new LazyURL(attr[1], baseUrl).href; | ||
break; | ||
} | ||
} | ||
} | ||
else if (token.type === 'link_open') | ||
{ | ||
for (const attr of token.attrs) | ||
{ | ||
if (attr[0] === 'href') | ||
{ | ||
//attr[1] = baseUrl + attr[1]; | ||
attr[1] = new LazyURL(attr[1], baseUrl2).href; | ||
break; | ||
} | ||
} | ||
} | ||
// Process recursively | ||
if (token.children !== null) | ||
{ | ||
_rewriteTokens(token.children); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.