Skip to content

Commit

Permalink
🪲 (markdown-render) The slot in the template has a higher priority
Browse files Browse the repository at this point in the history
Closes #83
  • Loading branch information
HcySunYang committed Jun 18, 2019
1 parent 61f1394 commit a3fb19b
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 12 deletions.
11 changes: 10 additions & 1 deletion packages/markdown-render/__test__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,16 @@ test('Proper rendering of the table header', () => {
describe: 'Table header',
backerDesc: '`<th>{{title}}</th>`',
bindings: {},
scoped: false
scoped: false,
target: 'template'
},
{
name: 'header',
describe: 'Table header',
backerDesc: '`<th>{{title}}</th>`',
bindings: {},
scoped: false,
target: 'script'
}
],
methods: [
Expand Down
20 changes: 20 additions & 0 deletions packages/markdown-render/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,26 @@ export class Render {
slotRender(slotsRes: SlotResult[]) {
const slotConfig = (this.options as RenderOptions).slots
let code = this.renderTabelHeader(slotConfig)

// If the template and script contain slots with the same name,
// only the slots in the template are rendered
const slotInTemplate: SlotResult[] = []
const slotInScript: SlotResult[] = []
slotsRes.forEach((slot: SlotResult) => {
slot.target === 'template'
? slotInTemplate.push(slot)
: slotInScript.push(slot)
})

slotsRes = slotInTemplate.concat(
slotInScript.filter(ss => {
for (let i = 0; i < slotInTemplate.length; i++) {
if (ss.name === slotInTemplate[i].name) return false
}
return true
})
)

slotsRes.forEach((slot: SlotResult) => {
const row: string[] = []
for (let i = 0; i < slotConfig.length; i++) {
Expand Down
6 changes: 5 additions & 1 deletion packages/parser/__test__/__fixtures__/namedSlot.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@

<script>
export default {
computed: {
hasBack() {
return this.$slots.header != null
}
}
}
</script>

Expand Down
15 changes: 9 additions & 6 deletions packages/parser/__test__/parseTemplate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,18 @@ test('Default slot with slot description in a v-if', () => {

test('Named slot with slot description', () => {
const sfc: AstResult = getAST('namedSlot.vue')
const mockOnSlot = jest.fn(() => {})
const options: ParserOptions = {
onSlot(slotRes) {
expect((slotRes as SlotResult).name).toBe('header')
expect((slotRes as SlotResult).describe).toBe('head slot')
expect((slotRes as SlotResult).backerDesc).toBe('Default Slot Content')
expect((slotRes as SlotResult).bindings).toEqual({})
}
onSlot: mockOnSlot
}
parseTemplate(sfc.templateAst, options)

expect(mockOnSlot.mock.calls.length).toBe(1)
const arg = mockOnSlot.mock.calls[0][0]
expect((arg as SlotResult).name).toBe('header')
expect((arg as SlotResult).describe).toBe('head slot')
expect((arg as SlotResult).backerDesc).toBe('Default Slot Content')
expect((arg as SlotResult).bindings).toEqual({})
})

test('Named slot with slot description and bingdings', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/parser/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export interface SlotResult {
backerDesc: string
bindings: AttrsMap
scoped: boolean
target: 'template' | 'script'
}

export interface ParserOptions {
Expand Down
6 changes: 4 additions & 2 deletions packages/parser/lib/parseJavascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ export function parseJavascript(ast: bt.File, options: ParserOptions = {}) {
? slotsComments.content.join('')
: '',
bindings: {},
scoped: true
scoped: true,
target: 'script'
}

options.onSlot(scopedSlots)
Expand Down Expand Up @@ -330,7 +331,8 @@ export function parseJavascript(ast: bt.File, options: ParserOptions = {}) {
? slotsComments.content.join('')
: '',
bindings: {},
scoped: false
scoped: false,
target: 'script'
}

options.onSlot(slotRes)
Expand Down
3 changes: 2 additions & 1 deletion packages/parser/lib/parseTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export function parseTemplate(templateAst: any, options: ParserOptions) {
describe: '',
backerDesc: '',
bindings: {},
scoped: false
scoped: false,
target: 'template'
}
slot.bindings = extractAndFilterAttr(templateAst.attrsMap)
if (slot.bindings.name) {
Expand Down
3 changes: 2 additions & 1 deletion packages/parser/lib/processRenderFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ export function determineChildren(
describe: '',
backerDesc: '',
scoped: false,
bindings: {}
bindings: {},
target: 'script'
}

const commentsRes: CommentResult = bt.isExpressionStatement(
Expand Down

0 comments on commit a3fb19b

Please sign in to comment.