postcss-markdown
Advanced tools
Comparing version 1.0.0-alpha.0 to 1.0.0-alpha.1
@@ -7,3 +7,5 @@ "use strict"; | ||
const remarkParser = unified().use(remarkParse); | ||
const remarkParser = unified().use(remarkParse, { | ||
commonmark: true, | ||
}); | ||
@@ -17,56 +19,83 @@ function extractStyles(source, opts) { | ||
const styles = []; | ||
visit(ast, (node, parent) => { | ||
if (node.type === "code") { | ||
if (parent !== ast) { | ||
// Currently it only supports root level blocks. | ||
let ignoreState = false; | ||
function addStyle(style) { | ||
if (ignoreState) { | ||
if (style.isMarkdown || /^<style/iu.test(style.content)) { | ||
return; | ||
} | ||
const block = node; | ||
/** @type {string | null} */ | ||
const lang = block.lang; | ||
if (!lang) { | ||
return; | ||
} | ||
const syntax = resolveSyntax(lang); | ||
if (!syntax) { | ||
return; | ||
} | ||
let startIndex = | ||
source.indexOf(block.lang, block.position.start.offset) + | ||
block.lang.length; | ||
if (block.value) { | ||
startIndex = source.indexOf(block.value, startIndex); | ||
} else { | ||
startIndex = source.indexOf("\n", startIndex) + 1; | ||
} | ||
styles.push({ | ||
startIndex, | ||
isMarkdown: true, | ||
content: source | ||
.slice(startIndex, block.position.end.offset) | ||
.replace(/[\t ]*`*$/, ""), | ||
lang: lang.toLowerCase(), | ||
syntax, | ||
}); | ||
} | ||
styles.push(style); | ||
} | ||
function processBlock(block) { | ||
/** @type {string | null} */ | ||
const lang = block.lang; | ||
if (!lang) { | ||
return; | ||
} | ||
if (node.type === "html") { | ||
if (!node.value) { | ||
return; | ||
} | ||
if (htmlInMd && /^<style/iu.test(node.value)) { | ||
const syntax = resolveSyntax("html"); | ||
if (!syntax) { | ||
return; | ||
const syntax = resolveSyntax(lang); | ||
if (!syntax) { | ||
return; | ||
} | ||
let startIndex = | ||
source.indexOf(block.lang, block.position.start.offset) + | ||
block.lang.length; | ||
startIndex = source.indexOf("\n", startIndex) + 1; | ||
const content = source | ||
.slice(startIndex, block.position.end.offset) | ||
.replace(/[\t ]*`*$/, ""); | ||
if ( | ||
block.value.replace(/\s+/g, " ").trim() !== | ||
content.replace(/\s+/g, " ").trim() | ||
) { | ||
return; | ||
} | ||
addStyle({ | ||
startIndex, | ||
isMarkdown: true, | ||
content, | ||
lang: lang.toLowerCase(), | ||
syntax, | ||
}); | ||
} | ||
function processHtml(node) { | ||
const syntax = resolveSyntax("html"); | ||
if (!syntax) { | ||
return; | ||
} | ||
const startIndex = node.position.start.offset; | ||
addStyle({ | ||
startIndex, | ||
isMarkdown: false, | ||
content: source.slice(startIndex, node.position.end.offset), | ||
lang: "html", | ||
syntax, | ||
}); | ||
} | ||
visit(ast, (node, parent) => { | ||
if (node.type === "code") { | ||
processBlock(node, parent); | ||
} else if (node.type === "html") { | ||
if (node.value) { | ||
const comment = /^<!--([\s\S]*)-->$/u.exec(node.value); | ||
if (comment) { | ||
const commentValue = comment[1]; | ||
if (commentValue.trim() === "postcss-ignore") { | ||
ignoreState = true; | ||
return; | ||
} | ||
} else { | ||
if (htmlInMd) { | ||
processHtml(node); | ||
} | ||
} | ||
const startIndex = node.position.start.offset; | ||
styles.push({ | ||
startIndex, | ||
isMarkdown: false, | ||
content: source.slice(startIndex, node.position.end.offset), | ||
lang: "html", | ||
syntax, | ||
}); | ||
} | ||
} | ||
ignoreState = false; | ||
}); | ||
@@ -73,0 +102,0 @@ |
{ | ||
"name": "postcss-markdown", | ||
"version": "1.0.0-alpha.0", | ||
"version": "1.0.0-alpha.1", | ||
"publishConfig": { | ||
@@ -64,5 +64,6 @@ "access": "public", | ||
"@ota-meshi/eslint-plugin": "^0.10.0", | ||
"autoprefixer": "^10.3.7", | ||
"chai": "^4.2.0", | ||
"codecov": "^3.8.1", | ||
"eslint": "^7.25.0", | ||
"eslint": "^8.0.0", | ||
"eslint-config-prettier": "^8.3.0", | ||
@@ -77,7 +78,7 @@ "eslint-plugin-eslint-comments": "^3.2.0", | ||
"eslint-plugin-yml": "^0.10.1", | ||
"mocha": "^8.2.1", | ||
"mocha": "^9.0.0", | ||
"mocha-chai-jest-snapshot": "^1.1.3", | ||
"nyc": "^15.1.0", | ||
"postcss-html": "^1.0.0-0", | ||
"postcss-less": "^3.1.0", | ||
"postcss-less": "^5.0.0", | ||
"postcss-scss": "^2.0.0", | ||
@@ -88,4 +89,4 @@ "postcss-styl": "^0.9.0", | ||
"stylelint-config-standard": "^23.0.0", | ||
"sugarss": "^2.0.0" | ||
"sugarss": "^4.0.0" | ||
} | ||
} |
@@ -37,3 +37,3 @@ # PostCSS Markdown Syntax | ||
const syntax = require("postcss-markdown")({ | ||
// Enable support for HTML (default: true) See: https://github.com/gucong3000/postcss-html | ||
// Enable support for HTML (default: true) | ||
htmlInMd: true, | ||
@@ -72,3 +72,3 @@ // syntax for parse scss (non-required options) | ||
```css | ||
::-webkit-input-placeholder { | ||
::-moz-placeholder { | ||
color: gray; | ||
@@ -79,5 +79,2 @@ } | ||
} | ||
::-ms-input-placeholder { | ||
color: gray; | ||
} | ||
::placeholder { | ||
@@ -132,2 +129,14 @@ color: gray; | ||
## Turning PostCSS off from within your Markdown | ||
PostCSS can be temporarily turned off by using special comments in your Markdown. For example: | ||
<pre><code><!-- postcss-ignore --> | ||
```css | ||
a { | ||
color: red; | ||
} | ||
``` | ||
</code></pre> | ||
## Linting with Stylelint | ||
@@ -137,2 +146,15 @@ | ||
You can use it by configuring your `stylelint` config as follows: | ||
```json | ||
{ | ||
"overrides": [ | ||
{ | ||
"files": ["*.md", "**/*.md"], | ||
"customSyntax": "postcss-markdown" | ||
} | ||
] | ||
} | ||
``` | ||
[stylelint]: https://stylelint.io/ |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
20578
509
156
0
25