Giter VIP home page Giter VIP logo

remark-attr's Introduction

remark-attr

This plugin adds support for custom attributes to Markdown syntax.

For security reasons, this plugin uses html-element-attributes. The use of JavaScript attributes (onload for example) is not allowed by default.

Default Syntax

Images :

![alt](img){attrs} / ![alt](img){ height=50 }

Links :

[rms with a computer](https://rms.sexy){rel="external"}

Autolink :

Email me at : <mailto:[email protected]>

Header (Atx) :

### This is a title
{style="color:red;"}

or

### This is a title {style="color:yellow;"}

If option enableAtxHeaderInline is set to `true` (default value).

Header :

This is a title
---------------
{style="color: pink;"}

Emphasis :

Npm stand for *node*{style="color:red"} packet manager.

Strong :

This is a **Unicorn**{awesome} !

Delete :

Your problem is ~~at line 18~~{style="color: grey"}. My mistake, it's at line 14.


Code :

~~~markdown
You can use the `fprintf`{language=c} function to format the output to a file.

Footnote (using remark-footnotes) :

This is a footnote[^ref]{style="opacity: 0.8;"}


[^ref]: And the reference.

rehype

At the moment it aims is to be used with rehype only, using remark-rehype.

[rms with a computer](https://rms.sexy){rel=external}

produces:

<a href="https://rms.sexy" rel="external">rms with a computer</a>

Installation

npm:

npm install remark-attr

Dependencies:

const unified = require('unified')
const remarkParse = require('remark-parse')
const stringify = require('rehype-stringify')
const remark2rehype = require('remark-rehype')
const remarkAttr = require('remark-attr')

Usage:

const testFile = `

Here a test :

![ache avatar](https://ache.one/res/ache.svg){ height=100 }

`

unified()
  .use(remarkParse)
  .use(remarkAttr)
  .use(remark2rehype)
  .use(stringify)
  .process( testFile, (err, file) => {
    console.log(String(file))
  } )

Output :

$ node index.js
<p>Here a test :</p>
<p><img src="https://ache.one/res/ache.svg" alt="ache avatar" height="100"></p>

API

remarkAttr([options])

Parse attributes of markdown elements.

remarkAttr.SUPPORTED_ELEMENTS

The list of currently supported elements.

['link', 'atxHeading', 'strong', 'emphasis', 'deletion', 'code', 'setextHeading']

['link', 'atxHeading', 'strong', 'emphasis', 'deletion', 'code', 'setextHeading', 'fencedCode', 'reference', 'footnoteCall', 'autoLink']

Options
options.allowDangerousDOMEventHandlers

Whether to allow the use of on-* attributes. They are depreciated and disabled by default for security reasons. Its a boolean (default: false). If allowed, DOM event handlers will be added to the global scope.

options.elements

The list of elements which the attributes should be parsed. It's a list of string, a sub-list of SUPPORTED_ELEMENTS. If you are confident enough you can add the name of a tokenizer that isn't officialy supported but remember that it will not have been tested.

options.extend

An object that extends the list of attributes supported for some elements.

Example : extend: {heading: ['original', 'quality', 'format', 'toc']}

With this configuration, if the scope permits it, 4 mores attributes will be supported for atxHeading elements.

options.scope

A string with the value global or specific or extented or none or every.

  • none will disable the plugin.
  • global will activate only the global attributes.
  • specific will activate global and specific attributes.
  • extended will add personalized tags for some elements.
  • permissive or every will allow every attributes (except dangerous one) on every elements supported.
options.enableAtxHeaderInline

Whether to allow atx headers with attributes on the same line.

### This is a title {style="color:yellow;"}

How does it works ?

This plugin extend the syntax of [remark-parse][remark-parse] by replacing old tokenizers by new one. The new tokenizers functions re-use the old tokenizers and md-attr-parser to parse the extended syntax.

So option.SUPPORTED_ELEMENTS are the names of the tokenizers and neither arbitrary names nor HTML tag names. Here is the related documentation.

License

Distributed under a MIT license.

remark-attr's People

Contributors

arobase-che avatar atomicpages avatar diomed avatar nd0ut avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

remark-attr's Issues

Attribute for <p> / paragraph?

Hi,

I'm unsure if this is an issue of MDX or remark-attr.

I can't appear to use:

This is a paragraph.{.class-name}

I tried adding 'p' and 'paragraph' to the options.elemets as well.

Is this intended?

Tags not added on images with Docusaurus

I'm using docusaurus which uses rehype/remark and added remark-attr. Other markups like links, headers, etc. are working fine but image tags just don't get any attributes added. I also tested the paragraph branch which works fine with paragraphs.

Is there any test I could do to help diagnose the problem with images further?
I'm using

![img](img.png){class="none"}

which leads to

<img alt="img" src="/assets/images/img-2ce0a3f91f9b6ea2d3149cbaab375986.png">

Images are obviously copied to another folder and the src attribute rewritten in the way. This might just be the reason.

There is a bug

Hello, when I use your Remark-attr I found an issue: let eaten = oldParser.call(self, eat, value, silent); can't work well during build (using webpack tool in React). The error is —— call can't use. BTW, I found your codes in github are different with npm(for instance, index.js). Which is the latest version? Thank you!

Fenced code?

Any reason this does not or cannot work with fenced code... for example:

```html{ style="..." }
<p>Hello</p>
```

classes?

is adding classes possible?

![Image](https://picsum.photos/4000/6016?image=949){:.className}

I didn't find anything in readme.

Support spans with attributes?

At the moment, the separate package remark-bracketed-spans supports the following syntax:

This is [a span]{#id .class key=value}

Would it make sense to include support for spans in remark-attr? Then it would contain everything related to attributes.

The functionality of remark-bracketed-spans wouldn’t have to be reimplemented, it could just be imported and re-exported (maybe as an option, to avoid breaking existing code).

'Missing parser to attach `remark-attr` [link] (to)

Hi,
I am trying to use remark and the module remark-attr for the first time in a project but I am getting the following error:

/node_modules/remark-attr/dist/index.js:352
    throw new Error('Missing parser to attach `remark-attr` [link] (to)');
          ^

Error: Missing parser to attach `remark-attr` [link] (to)
    at Function.remarkAttr (/project/node_modules/remark-attr/dist/index.js:352:11)
    at Function.freeze (file:///project/node_modules/unified/lib/index.js:136:36)
    at Function.process (file:///project/node_modules/unified/lib/index.js:375:15)
    at file:///project/index.js:21:6
    at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:541:24)
    at async loadESM (node:internal/process/esm_loader:91:5)
    at async handleMainPromise (node:internal/modules/run_main:65:12)

Node.js v18.7.0

The package.json is:

{
  "name": "remark-attr",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "rehype-stringify": "^9.0.3",
    "remark-attr": "^0.11.1",
    "remark-parse": "^10.0.1",
    "remark-rehype": "^10.1.0",
    "unified": "^10.1.2"
  },
  "type": "module"
}

The index.js is:

import {unified} from 'unified'
import remarkParse from 'remark-parse'
import stringify from 'rehype-stringify'
import remark2rehype from 'remark-rehype'
import remarkAttr from 'remark-attr'


const testFile = `

Here a test :

![ache avatar](https://ache.one/res/ache.svg){ height=100 }

`

unified()
    .use(remarkParse)
    .use(remarkAttr)
    .use(remark2rehype)
    .use(stringify)
    .process(testFile, (err, file) => {
        console.log(String(file))
    })

Can you help?

I really wanted to use the remark and rehype but if there is no solution I really need to find another alternative because the markdown will use a lot of attributes in markdown.

Please let know if there is a solution.
Thank you for your time

ATX-style cannot specify attributes if the header has inline syntax

ATX-style cannot specify attributes if the header has inline syntax.

Markdown:

# Header 1 {#header-1}

# Header 2
{#header-2}

# Header 3 **with text** {#header-3}

Expected:

<h1 id="header-1">Header 1</h1>
<h1 id="header-2">Header 2</h1>
<h1 id="header-3">Header 3 <strong>with text</strong></h1>

Actual:

<h1 id="header-1">Header 1</h1>
<h1 id="header-2">Header 2</h1>
<h1>Header 3 <strong>with text</strong> {#header-3}</h1>

{# header-3} remains as text content.

remark@next (13)

Hi!

remark is switching to a new parser (and compiler) internally (micromark, remarkjs/remark#536), which will break this plugin.
Keep an eye out for remark-footnotes, remark-frontmatter, remark-gfm, remark-math, and other syntax plugins similar to this one, which could serve as inspiration for this plugin. Typically, these syntax plugins will be small wrapper code around micromark and mdast extensions btw. Also: feel free to ask me questions!

Support angle-bracket links

Would be cool, if this package supported adding attributes to angle-bracket links like

<example.com>{.some .classes}

Stop looking for attrs in the fenced code

Happy new year!

Do you while we can get rid of this code: https://github.com/arobase-che/remark-attr/blob/master/src/index.js#L194

If we still need it we might need to improve the algorithm here. I've found several cases where the attrs are being picked up when they shouldn't... for example:

```html
code {info=string}
This is an awesome code
```

will apply the info="string" attribute to the outer code block.

My workaround, for now, is to add attr markup to prevent remark-attr from diving into the value:

```html {}
code {info=string}
This is an awesome code
```

Are image elements supported?

I use this plugin with react-markdown

I can't seem to get attributes for images, other elements such as <em/> work fine
Are images supported?

Img is not listed in supported.elements but the readme includes the following example:

![alt](img){attrs} / ![alt](img){ height=50 }

Support for linkReference

Could you add support for the linkReference node? I tried forking and extending the plugin myself, but I ultimately couldn't make it work.

The problem

The following markdown:

[first link](http://example.com/1){hreflang="en"}   <!-- works -->
[second link][reference]{hreflang="en"}             <!-- doesn't work -->
[third link][]{hreflang="en"}                       <!-- doesn't work -->

[reference]:  http://example.com/2
[third link]: http://example.com/3

produces the following HTML output:

<a href="http://example.com/1" hreflang="en">first link</a>
<a href="http://example.com/2">second link</a>{hreflang="en"}
<a href="http://example.com/3">third link</a>{hreflang="en"}

instead of:

<a href="http://example.com/1" hreflang="en">first link</a>
<a href="http://example.com/2" hreflang="en">second link</a>
<a href="http://example.com/3" hreflang="en">third link</a>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.