-
-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: transform vue template with posthtml (#479)
* use posthtml * remove legacy test * add test * use custom path * add hook transformTemplate * Replace hook transformTemplate with chainTemplate * fix hook type * fix * tweaks
- Loading branch information
Showing
19 changed files
with
289 additions
and
124 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
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.
This file was deleted.
Oops, something went wrong.
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,86 @@ | ||
// eslint-disable | ||
// Extracted from https://github.com/calvinmetcalf/rollup-plugin-node-builtins/blob/master/src/es6/path.js | ||
|
||
function normalizeArray(parts, allowAboveRoot) { | ||
// if the path tries to go above the root, `up` ends up > 0 | ||
var up = 0 | ||
for (var i = parts.length - 1; i >= 0; i--) { | ||
var last = parts[i] | ||
if (last === '.') { | ||
parts.splice(i, 1) | ||
} else if (last === '..') { | ||
parts.splice(i, 1) | ||
up++ | ||
} else if (up) { | ||
parts.splice(i, 1) | ||
up-- | ||
} | ||
} | ||
|
||
// if the path is allowed to go above the root, restore leading ..s | ||
if (allowAboveRoot) { | ||
for (; up--; up) { | ||
parts.unshift('..') | ||
} | ||
} | ||
|
||
return parts | ||
} | ||
|
||
// Split a filename into [root, dir, basename, ext], unix version | ||
// 'root' is just a slash, or nothing. | ||
var splitPathRe = /^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/ | ||
var splitPath = function(filename) { | ||
return splitPathRe.exec(filename).slice(1) | ||
} | ||
|
||
// path.normalize(path) | ||
// posix version | ||
function normalize(path) { | ||
var isPathAbsolute = isAbsolute(path), | ||
trailingSlash = path.substr(-1) === '/' | ||
|
||
// Normalize the path | ||
path = normalizeArray( | ||
path.split('/').filter(Boolean), | ||
!isPathAbsolute | ||
).join('/') | ||
|
||
if (!path && !isPathAbsolute) { | ||
path = '.' | ||
} | ||
if (path && trailingSlash) { | ||
path += '/' | ||
} | ||
|
||
return (isPathAbsolute ? '/' : '') + path | ||
} | ||
// posix version | ||
function isAbsolute(path) { | ||
return path.charAt(0) === '/' | ||
} | ||
|
||
// posix version | ||
function join(...paths) { | ||
return normalize(paths.filter(p => typeof p === 'string').join('/')) | ||
} | ||
|
||
function dirname(path) { | ||
var result = splitPath(path), | ||
root = result[0], | ||
dir = result[1] | ||
|
||
if (!root && !dir) { | ||
// No dirname whatsoever | ||
return '.' | ||
} | ||
|
||
if (dir) { | ||
// It has a dirname, strip trailing slash | ||
dir = dir.substr(0, dir.length - 1) | ||
} | ||
|
||
return root + dir | ||
} | ||
|
||
export { dirname, join } |
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
31 changes: 31 additions & 0 deletions
31
packages/saber/vue-renderer/lib/template-plugins/__test__/link.test.js
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,31 @@ | ||
const posthtml = require('posthtml') | ||
const plugin = require('../link') | ||
|
||
const transform = source => | ||
posthtml([plugin()]) | ||
.process(source, { | ||
recognizeSelfClosing: true | ||
}) | ||
.then(res => res.html) | ||
|
||
test('basic', async () => { | ||
const html = await transform(` | ||
<a href="foo">foo</a> | ||
<a href="https://example.com">foo</a> | ||
<a href="mailto:[email protected]">foo</a> | ||
<saber-link to="/foo">foo</saber-link> | ||
<saber-link :to="foo">foo</saber-link> | ||
`) | ||
expect(html).toBe(` | ||
<saber-link :to="$saber.getPageLink('foo')">foo</saber-link> | ||
<a target="_blank" rel="noopener noreferrer" href="https://example.com">foo</a> | ||
<a href="mailto:[email protected]">foo</a> | ||
<saber-link :to="$saber.getPageLink('/foo')">foo</saber-link> | ||
<saber-link :to="foo">foo</saber-link> | ||
`) | ||
}) | ||
|
||
test('ignore', async () => { | ||
const html = await transform(`<a href="foo" saber-ignore>foo</a>`) | ||
expect(html).toBe(`<a href="foo">foo</a>`) | ||
}) |
Oops, something went wrong.