-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add built-in component embed (#218)
- Loading branch information
1 parent
c379ed2
commit 5172061
Showing
26 changed files
with
336 additions
and
38 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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
hidden: true | ||
--- | ||
This is foo |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { toolkit } from '@docgeni/toolkit'; | ||
import * as path from 'path'; | ||
import fm from 'front-matter'; | ||
import { RendererExtension, TokenizerExtension } from 'marked'; | ||
import { compatibleNormalize } from './utils'; | ||
|
||
export function getEmbedBody(input: string, url: string) { | ||
// TODO: add hash | ||
return fm(compatibleNormalize(input)).body; | ||
} | ||
|
||
export interface EmbedToken { | ||
type: 'embed'; | ||
raw: string; | ||
src: string; | ||
tokens: []; | ||
message?: string; | ||
} | ||
|
||
export const embed: TokenizerExtension & RendererExtension = { | ||
name: 'embed', | ||
level: 'block', | ||
start(src: string) { | ||
return src.match(/<embed/)?.index; | ||
}, | ||
tokenizer(src: string, tokens: any[]) { | ||
const rule = /^<embed\W*src=['"]([^"']*)\W*\/?>(<\/embed>?)/gi; // Regex for the complete token | ||
const match = rule.exec(src); | ||
if (match) { | ||
const token: EmbedToken = { | ||
// Token to generate | ||
type: 'embed', | ||
raw: match[0], | ||
src: match[1].trim(), | ||
tokens: [] | ||
}; | ||
|
||
// eslint-disable-next-line dot-notation | ||
const absFilePath: string = this.lexer.options['absFilePath']; | ||
const absDirPath = path.dirname(absFilePath); | ||
const nodeAbsPath = path.resolve(absDirPath, token.src); | ||
if (nodeAbsPath !== absFilePath && toolkit.fs.pathExistsSync(nodeAbsPath)) { | ||
const content = toolkit.fs.readFileSync(nodeAbsPath).toString(); | ||
this.lexer.blockTokens(getEmbedBody(content, token.src), token.tokens); | ||
} else { | ||
token.message = `can't resolve path ${token.src}`; | ||
} | ||
return token; | ||
} | ||
}, | ||
renderer(token: EmbedToken) { | ||
return `<div embed src="${token.src}">${token.message ? token.message : this.parser.parse(token.tokens)}</div>`; | ||
} | ||
}; |
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 +1,2 @@ | ||
export * from './markdown'; | ||
export * from './utils'; |
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,9 +1,73 @@ | ||
import { toolkit } from '@docgeni/toolkit'; | ||
import { EOL } from 'os'; | ||
import { loadFixture, FixtureResult } from '../testing/fixture-loader'; | ||
import { Markdown } from './markdown'; | ||
import path from 'path'; | ||
import marked from 'marked'; | ||
import { compatibleNormalize } from './utils'; | ||
|
||
describe('markdown', () => { | ||
const data = ['```bash', 'ng add alib', '```'].join('\n'); | ||
it('run', () => { | ||
it('should get correct result for base and ng command', () => { | ||
const data = ['```bash', 'ng add alib', '```'].join('\n'); | ||
const result = Markdown.toHTML(data); | ||
expect(result).not.toContain('%20'); | ||
expect(result).toContain(`<span class="token function">ng</span>`); | ||
}); | ||
|
||
describe('full', () => { | ||
let fixture: FixtureResult; | ||
|
||
beforeAll(async () => { | ||
fixture = await loadFixture('markdown-full'); | ||
}); | ||
|
||
it('should transform full marked success', () => { | ||
const output = Markdown.toHTML(fixture.src['hello.md'], { | ||
absFilePath: fixture.getSrcPath('hello.md') | ||
}); | ||
expect(output).toContain(`This is an <h1> tag`); | ||
expect(output).toContain(`This is an <h2> tag`); | ||
expect(output).toContain(`<example name="my-example" ></example>`); | ||
}); | ||
|
||
it('should transform custom tag success', () => { | ||
const output = Markdown.toHTML('<label><div>content</div></label>', {}); | ||
expect(output).toContain(`<div class="dg-paragraph"><label><div>content</div></label></div>`); | ||
}); | ||
}); | ||
|
||
describe('link', () => { | ||
it('should renderer link success', () => { | ||
const output = Markdown.toHTML('[book](./book)', {}); | ||
expect(output).toContain(`<a href="./book">book</a>`); | ||
}); | ||
|
||
it('should add target="_blank" for external link', () => { | ||
const output = Markdown.toHTML('[book](http://pingcode.com/book)', {}); | ||
expect(output).toContain(`<a target="_blank" href="http://pingcode.com/book">book</a>`); | ||
}); | ||
}); | ||
|
||
describe('embed', () => { | ||
let fixture: FixtureResult; | ||
|
||
beforeAll(async () => { | ||
fixture = await loadFixture('markdown-embed'); | ||
}); | ||
|
||
it('should transform embed success', () => { | ||
const output = Markdown.toHTML(fixture.src['hello.md'], { absFilePath: fixture.getSrcPath('hello.md') }); | ||
expect(compatibleNormalize(output).trim()).toEqual(fixture.getOutputContent('hello.html')); | ||
}); | ||
|
||
xit('should throw error when embed ref self', () => { | ||
const output = Markdown.toHTML(fixture.src['embed-self.md'], { absFilePath: fixture.getSrcPath('embed-self.md') }); | ||
expect(output).toContain(`<div embed src="./embed-self.md">can't resolve path ./embed-self.md</div>`); | ||
}); | ||
|
||
xit('should throw error when embed ref-not-found.md', () => { | ||
const output = Markdown.toHTML(fixture.src['not-found.md'], { absFilePath: fixture.getSrcPath('not-found.md') }); | ||
expect(output).toContain(`<div embed src="./ref-not-found.md">can't resolve path ./ref-not-found.md</div>`); | ||
}); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function compatibleNormalize(input: string) { | ||
return input.replace(/\r\n|\r/g, '\n').replace(/\t/g, ' '); | ||
} |
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
16 changes: 16 additions & 0 deletions
16
packages/core/test/fixtures/markdown-embed/output/hello.html
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,16 @@ | ||
<p>Foo:</p> | ||
<div embed src="./foo.md"> | ||
<h2 id="foo" class="docs-header-link"> | ||
<span header-link="foo"></span> | ||
Foo | ||
</h2> | ||
<p>This is foo</p> | ||
</div><p>this is newline-- | ||
Bar:</p> | ||
<div embed src="./bar.md"> | ||
<h2 id="bar" class="docs-header-link"> | ||
<span header-link="bar"></span> | ||
Bar | ||
</h2> | ||
<p>This is <em>bar</em></p> | ||
</div> |
11 changes: 11 additions & 0 deletions
11
packages/core/test/fixtures/markdown-embed/output/hello.md
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,11 @@ | ||
Foo: | ||
## Foo | ||
|
||
This is foo | ||
|
||
this is newline-- | ||
Bar: | ||
## Bar | ||
|
||
This is bar | ||
|
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,3 @@ | ||
## Bar | ||
|
||
This is *bar* |
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 @@ | ||
<embed src="./embed-self.md"></embed> |
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,3 @@ | ||
## Foo | ||
|
||
This is foo |
Oops, something went wrong.