-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
6,800 additions
and
1,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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { defineBuildConfig } from 'unbuild' | ||
|
||
export default defineBuildConfig({ | ||
rollup: { | ||
emitCJS: true, | ||
}, | ||
rootDir: './lib', | ||
outDir: '../dist', | ||
entries: ['index.js'], | ||
declaration: true, | ||
}) |
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,102 @@ | ||
'use strict'; | ||
|
||
const plugin = (options = {}) => (tree) => { | ||
options.tag = options.tag || "outlook"; | ||
const attributes = ["only", "not", "lt", "lte", "gt", "gte"]; | ||
const versions = { | ||
2e3: 9, | ||
2002: 10, | ||
2003: 11, | ||
2007: 12, | ||
2010: 14, | ||
2013: 15, | ||
2016: 16, | ||
2019: 16 | ||
}; | ||
const process = (node) => { | ||
if (node.tag === `not-${options.tag}`) { | ||
node.tag = false; | ||
node.content = `<!--[if !mso]><!-->${tree.render(node.content)}<!--<![endif]-->`; | ||
return node; | ||
} | ||
if (node.tag !== options.tag) { | ||
return node; | ||
} | ||
node.tag = false; | ||
if (Array.isArray(node.content)) { | ||
for (const line of node.content) { | ||
if (line.tag && [options.tag, `not-${options.tag}`].includes(line.tag)) { | ||
line.tag = false; | ||
} | ||
} | ||
} | ||
if (!hasAttributes(node.attrs, attributes)) { | ||
node.content = `<!--[if mso]>${tree.render(node.content)}<![endif]-->`; | ||
return node; | ||
} | ||
const foundAttrs = intersectAttributes(node.attrs, attributes); | ||
const firstAttr = foundAttrs[0]; | ||
const secondAttr = foundAttrs[1] || false; | ||
if (["only", "not"].includes(firstAttr)) { | ||
const items = node.attrs[firstAttr].split(","); | ||
const negation = firstAttr === "not" ? "!" : ""; | ||
if (items.length > 1) { | ||
let conditions = []; | ||
for (const item of items) { | ||
if (versions[item]) { | ||
conditions.push(`(mso ${versions[item]})`); | ||
} | ||
} | ||
conditions = [...new Set(conditions)]; | ||
conditions = conditions.length > 1 ? conditions.join("|") : conditions[0].replace(/[()]/g, ""); | ||
if (firstAttr === "not") { | ||
node.content = `<!--[if ${negation}(${conditions})]>${tree.render(node.content)}<![endif]-->`; | ||
} else { | ||
node.content = `<!--[if ${conditions}]>${tree.render(node.content)}<![endif]-->`; | ||
} | ||
} else if (items.length === 1 && versions[items[0]]) { | ||
node.content = `<!--[if ${negation}mso ${versions[items[0]]}]>${tree.render(node.content)}<![endif]-->`; | ||
} | ||
return node; | ||
} | ||
if (arraysMatch(foundAttrs, ["gt", "lt"]) || arraysMatch(foundAttrs, ["gt", "lte"]) || arraysMatch(foundAttrs, ["gte", "lte"]) || arraysMatch(foundAttrs, ["gte", "lt"]) || arraysMatch(foundAttrs, ["lt", "gt"]) || arraysMatch(foundAttrs, ["lt", "gte"]) || arraysMatch(foundAttrs, ["lte", "gte"]) || arraysMatch(foundAttrs, ["lte", "gt"])) { | ||
if (versions[node.attrs[firstAttr]] && versions[node.attrs[secondAttr]]) { | ||
node.content = `<!--[if (${firstAttr} mso ${versions[node.attrs[firstAttr]]})&(${secondAttr} mso ${versions[node.attrs[secondAttr]]})]>${tree.render(node.content)}<![endif]-->`; | ||
} | ||
return node; | ||
} | ||
if (firstAttr === "lt" && versions[node.attrs.lt]) { | ||
node.content = `<!--[if lt mso ${versions[node.attrs.lt]}]>${tree.render(node.content)}<![endif]-->`; | ||
return node; | ||
} | ||
if (firstAttr === "lte" && versions[node.attrs.lte]) { | ||
node.content = `<!--[if lte mso ${versions[node.attrs.lte]}]>${tree.render(node.content)}<![endif]-->`; | ||
return node; | ||
} | ||
if (firstAttr === "gt" && versions[node.attrs.gt]) { | ||
node.content = `<!--[if gt mso ${versions[node.attrs.gt]}]>${tree.render(node.content)}<![endif]-->`; | ||
return node; | ||
} | ||
if (firstAttr === "gte" && versions[node.attrs.gte]) { | ||
node.content = `<!--[if gte mso ${versions[node.attrs.gte]}]>${tree.render(node.content)}<![endif]-->`; | ||
return node; | ||
} | ||
return node; | ||
}; | ||
return tree.walk(process); | ||
}; | ||
const hasAttributes = (o, a) => o ? Object.keys(o).some((r) => a.includes(r)) : false; | ||
const intersectAttributes = (o, a) => o ? Object.keys(o).filter((r) => a.includes(r)) : false; | ||
const arraysMatch = (first, second) => { | ||
if (first.length !== second.length) { | ||
return false; | ||
} | ||
for (const [i, element] of first.entries()) { | ||
if (element !== second[i]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}; | ||
|
||
module.exports = plugin; |
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 @@ | ||
type PluginOptions = { | ||
/** | ||
The name of the tag for the plugin to use. | ||
@default 'outlook' | ||
@example | ||
```js | ||
import posthtml from 'posthtml' | ||
import posthtmlMso from 'posthtml-mso' | ||
const html = ` | ||
<mso only="2013">Show in Outlook 2013</mso> | ||
<not-mso>Hide from Outlook</not-mso> | ||
` | ||
posthtml([ | ||
posthtmlMso({tag: 'mso'}) | ||
]) | ||
.process(html) | ||
.then(result => console.log(result.html)) | ||
// Result: | ||
// <!--[if mso 15]>Show in Outlook 2013<![endif]--> | ||
// <!--[if !mso]><!-->Hide from Outlook<!--<![endif]--> | ||
``` | ||
*/ | ||
tag?: string; | ||
}; | ||
|
||
export type { PluginOptions }; |
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 @@ | ||
type PluginOptions = { | ||
/** | ||
The name of the tag for the plugin to use. | ||
@default 'outlook' | ||
@example | ||
```js | ||
import posthtml from 'posthtml' | ||
import posthtmlMso from 'posthtml-mso' | ||
const html = ` | ||
<mso only="2013">Show in Outlook 2013</mso> | ||
<not-mso>Hide from Outlook</not-mso> | ||
` | ||
posthtml([ | ||
posthtmlMso({tag: 'mso'}) | ||
]) | ||
.process(html) | ||
.then(result => console.log(result.html)) | ||
// Result: | ||
// <!--[if mso 15]>Show in Outlook 2013<![endif]--> | ||
// <!--[if !mso]><!-->Hide from Outlook<!--<![endif]--> | ||
``` | ||
*/ | ||
tag?: string; | ||
}; | ||
|
||
export type { PluginOptions }; |
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 @@ | ||
type PluginOptions = { | ||
/** | ||
The name of the tag for the plugin to use. | ||
@default 'outlook' | ||
@example | ||
```js | ||
import posthtml from 'posthtml' | ||
import posthtmlMso from 'posthtml-mso' | ||
const html = ` | ||
<mso only="2013">Show in Outlook 2013</mso> | ||
<not-mso>Hide from Outlook</not-mso> | ||
` | ||
posthtml([ | ||
posthtmlMso({tag: 'mso'}) | ||
]) | ||
.process(html) | ||
.then(result => console.log(result.html)) | ||
// Result: | ||
// <!--[if mso 15]>Show in Outlook 2013<![endif]--> | ||
// <!--[if !mso]><!-->Hide from Outlook<!--<![endif]--> | ||
``` | ||
*/ | ||
tag?: string; | ||
}; | ||
|
||
export type { PluginOptions }; |
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,100 @@ | ||
const plugin = (options = {}) => (tree) => { | ||
options.tag = options.tag || "outlook"; | ||
const attributes = ["only", "not", "lt", "lte", "gt", "gte"]; | ||
const versions = { | ||
2e3: 9, | ||
2002: 10, | ||
2003: 11, | ||
2007: 12, | ||
2010: 14, | ||
2013: 15, | ||
2016: 16, | ||
2019: 16 | ||
}; | ||
const process = (node) => { | ||
if (node.tag === `not-${options.tag}`) { | ||
node.tag = false; | ||
node.content = `<!--[if !mso]><!-->${tree.render(node.content)}<!--<![endif]-->`; | ||
return node; | ||
} | ||
if (node.tag !== options.tag) { | ||
return node; | ||
} | ||
node.tag = false; | ||
if (Array.isArray(node.content)) { | ||
for (const line of node.content) { | ||
if (line.tag && [options.tag, `not-${options.tag}`].includes(line.tag)) { | ||
line.tag = false; | ||
} | ||
} | ||
} | ||
if (!hasAttributes(node.attrs, attributes)) { | ||
node.content = `<!--[if mso]>${tree.render(node.content)}<![endif]-->`; | ||
return node; | ||
} | ||
const foundAttrs = intersectAttributes(node.attrs, attributes); | ||
const firstAttr = foundAttrs[0]; | ||
const secondAttr = foundAttrs[1] || false; | ||
if (["only", "not"].includes(firstAttr)) { | ||
const items = node.attrs[firstAttr].split(","); | ||
const negation = firstAttr === "not" ? "!" : ""; | ||
if (items.length > 1) { | ||
let conditions = []; | ||
for (const item of items) { | ||
if (versions[item]) { | ||
conditions.push(`(mso ${versions[item]})`); | ||
} | ||
} | ||
conditions = [...new Set(conditions)]; | ||
conditions = conditions.length > 1 ? conditions.join("|") : conditions[0].replace(/[()]/g, ""); | ||
if (firstAttr === "not") { | ||
node.content = `<!--[if ${negation}(${conditions})]>${tree.render(node.content)}<![endif]-->`; | ||
} else { | ||
node.content = `<!--[if ${conditions}]>${tree.render(node.content)}<![endif]-->`; | ||
} | ||
} else if (items.length === 1 && versions[items[0]]) { | ||
node.content = `<!--[if ${negation}mso ${versions[items[0]]}]>${tree.render(node.content)}<![endif]-->`; | ||
} | ||
return node; | ||
} | ||
if (arraysMatch(foundAttrs, ["gt", "lt"]) || arraysMatch(foundAttrs, ["gt", "lte"]) || arraysMatch(foundAttrs, ["gte", "lte"]) || arraysMatch(foundAttrs, ["gte", "lt"]) || arraysMatch(foundAttrs, ["lt", "gt"]) || arraysMatch(foundAttrs, ["lt", "gte"]) || arraysMatch(foundAttrs, ["lte", "gte"]) || arraysMatch(foundAttrs, ["lte", "gt"])) { | ||
if (versions[node.attrs[firstAttr]] && versions[node.attrs[secondAttr]]) { | ||
node.content = `<!--[if (${firstAttr} mso ${versions[node.attrs[firstAttr]]})&(${secondAttr} mso ${versions[node.attrs[secondAttr]]})]>${tree.render(node.content)}<![endif]-->`; | ||
} | ||
return node; | ||
} | ||
if (firstAttr === "lt" && versions[node.attrs.lt]) { | ||
node.content = `<!--[if lt mso ${versions[node.attrs.lt]}]>${tree.render(node.content)}<![endif]-->`; | ||
return node; | ||
} | ||
if (firstAttr === "lte" && versions[node.attrs.lte]) { | ||
node.content = `<!--[if lte mso ${versions[node.attrs.lte]}]>${tree.render(node.content)}<![endif]-->`; | ||
return node; | ||
} | ||
if (firstAttr === "gt" && versions[node.attrs.gt]) { | ||
node.content = `<!--[if gt mso ${versions[node.attrs.gt]}]>${tree.render(node.content)}<![endif]-->`; | ||
return node; | ||
} | ||
if (firstAttr === "gte" && versions[node.attrs.gte]) { | ||
node.content = `<!--[if gte mso ${versions[node.attrs.gte]}]>${tree.render(node.content)}<![endif]-->`; | ||
return node; | ||
} | ||
return node; | ||
}; | ||
return tree.walk(process); | ||
}; | ||
const hasAttributes = (o, a) => o ? Object.keys(o).some((r) => a.includes(r)) : false; | ||
const intersectAttributes = (o, a) => o ? Object.keys(o).filter((r) => a.includes(r)) : false; | ||
const arraysMatch = (first, second) => { | ||
if (first.length !== second.length) { | ||
return false; | ||
} | ||
for (const [i, element] of first.entries()) { | ||
if (element !== second[i]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}; | ||
|
||
export { plugin as default }; |
Oops, something went wrong.