├── .gitignore ├── package.json ├── LICENSE ├── @.js └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Node build artifacts 2 | node_modules 3 | npm-debug.log 4 | 5 | # Local development 6 | *.env 7 | *.dev 8 | .DS_Store 9 | 10 | # Docker 11 | Dockerfile 12 | docker-compose.yml 13 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "at.js", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "Hosein2398 (google.com)", 10 | "license": "MIT", 11 | "devDependencies": { 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (C) 2017 Hosein barzegaran 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /@.js: -------------------------------------------------------------------------------- 1 | !(function () { 2 | window.atJs = function (conf) { 3 | if (!conf.baseUrl) { 4 | throw "@js : You should define a -baseUrl- ."; 5 | } 6 | if (typeof conf.text == 'undefined' && typeof conf.tagName == 'undefined') { 7 | throw "@Js : You forgot to define a -text- or -tagName-"; 8 | } 9 | if (conf.tagName && conf.text) { 10 | throw "@Js: You can't have a -text- and -tagName- together , you should make use of one of them."; 11 | } 12 | if (conf.text && conf.changeDom) { 13 | throw "@Js : you can't have -text- and -changeDom- together , -changeDom- is only allowed with tagName"; 14 | } 15 | if (typeof conf.tagName == 'undefined') { 16 | var tagContent = conf.text; 17 | } else { 18 | var tagName = document.querySelector(conf.tagName); 19 | var tagContent = tagName.innerText; 20 | } 21 | var atRegex = /@[^\s]+(?=(\s|.))/g; 22 | var getAllMatches = tagContent.match(atRegex); 23 | 24 | for (var i = 0; i < getAllMatches.length; i++) { 25 | var regy = new RegExp(getAllMatches[i], "g"); 26 | var userName = getAllMatches[i].substr(1, getAllMatches[i].length); 27 | tagContent = tagContent.replace(regy, "" + getAllMatches[i] + ""); 28 | } 29 | if (conf.changeDom) { 30 | tagName.innerHTML = tagContent; 31 | } else { 32 | return tagContent; 33 | } 34 | } 35 | })() -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # @JS 3 | Very lightweight tool to change your @ prefixed strings into links. 4 | 5 | # Usage 6 | Load it inside you html file then: 7 | ```Js 8 | atJs({ 9 | tagName : "#test", 10 | baseUrl: "www.mysitename.com/users/" 11 | }); 12 | ``` 13 | It will get #test's content and replace it's @s with links with href of baseUrl and users name and end of that. 14 | For instance , if you have this inside you #test tag: 15 | ``` 16 | Here is a test for some users name like @Tom and @Jim. 17 | ``` 18 | That will turn into this: 19 | ``` 20 | Here is a test for some users name like @Tom and @Jim. 21 | ``` 22 | ## Options 23 | `tagName` : It's query selector that defines where you want to get data from. 24 | 25 | `text` : You can also pass in text , you have to use tagName or text , can't use both. 26 | 27 | `changeDom` : If this is set to true after adding links , tag's content would change too. It's obviuse that this option should only be used when a tagName is provided. 28 | 29 | `baseUrl` : Defines url you want to attache your names to. 30 | 31 | ## API 32 | ##### atJs([options]) 33 | Returns `string` if `changeDom` is set to false. 34 | 35 | #### options: 36 | Type: `Object` 37 | Available options : `tagName` , `text` , `changeDom` , `baseUrl` . 38 | 39 | ##### tagName: 40 | Type : `domNode` 41 | Required : `true` (if `test` is not defined) 42 | 43 | ##### text: 44 | Type : `String` 45 | Required : `true` (if `tagName` is not defined) 46 | 47 | ##### baseUrl: 48 | Type : `String` 49 | Required : `true` 50 | 51 | ##### changeDom: 52 | Type : `Boolean` --------------------------------------------------------------------------------